{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# langchain"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上下文精度处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(id='d2b4ce63-3863-49ee-b1cb-05e9d4831c9c', metadata={}, page_content='篮球是一项伟大的运动。'), Document(id='1315b6da-d8c7-42f2-843b-953703b3b83b', metadata={}, page_content='我非常喜欢去看电影。'), Document(id='9ef95490-ce83-449a-af81-b4a8b2e3ceb2', metadata={}, page_content='凯尔特人队是我最喜欢的球队。')]\n",
      "篮球是一项伟大的运动。\n",
      "我非常喜欢去看电影。\n",
      "凯尔特人队是我最喜欢的球队。\n"
     ]
    }
   ],
   "source": [
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_community.vectorstores.utils import DistanceStrategy\n",
    "\n",
    "\n",
    "query = '凯尔特'\n",
    "passages = [\n",
    "    \"篮球是一项伟大的运动。\",\n",
    "    \"带我飞往月球是我最喜欢的歌曲之一。\",\n",
    "    \"凯尔特人队是我最喜欢的球队。\",\n",
    "    \"这是一篇关于波士顿凯尔特人的文件。\",\n",
    "    \"我非常喜欢去看电影。\",\n",
    "    \"波士顿凯尔特人队以20分的优势赢得了比赛。\",\n",
    "    \"这只是一段随机的文字。\",\n",
    "    \"《艾尔登之环》是过去15年最好的游戏之一。\",\n",
    "    \"L.科内特是凯尔特人队最好的球员之一。\",\n",
    "    '我不喜欢睡觉'\n",
    "    \"拉里.伯德是一位标志性的NBA球员。\"\n",
    "    ]\n",
    "  \n",
    "# init embedding model\n",
    "embed_model = HuggingFaceEmbeddings(\n",
    "   # model_name='maidalun1020/bce-embedding-base_v1',\n",
    "    model_name='all-MiniLM-L6-v2',\n",
    "    model_kwargs = {'device': 'cpu'},\n",
    "   encode_kwargs= {'batch_size': 64, 'normalize_embeddings': True}\n",
    ")\n",
    "# example #1. extract embeddings\n",
    "query_embedding = embed_model.embed_query(query)\n",
    "passages_embeddings = embed_model.embed_documents(passages)\n",
    "\n",
    "# example #2. langchain retriever example\n",
    "faiss_vectorstore = FAISS.from_texts(\n",
    "    passages, \n",
    "    embed_model, \n",
    "    distance_strategy=DistanceStrategy.MAX_INNER_PRODUCT)\n",
    "\n",
    "retriever = faiss_vectorstore.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "\n",
    "related_passages = retriever.invoke(query)\n",
    "\n",
    "print(related_passages)\n",
    "\n",
    "ques = FAISS.from_texts(passages,embed_model).as_retriever(search_kwargs={'k':3})\n",
    "doc = ques.get_relevant_documents(query)\n",
    "\n",
    "# 将doc的page_content的内容输出\n",
    "\n",
    "for content in doc:\n",
    "    print(content.page_content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(id='baeab1ff-c33c-428f-9c07-7eac6a8bc818', metadata={}, page_content='篮球是一项伟大的运动。'),\n",
       " Document(id='3d47e6b4-b9dc-4dc0-a4c7-4d62e5b7f50b', metadata={}, page_content='凯尔特人队是我最喜欢的球队。'),\n",
       " Document(id='8ab6b298-8eac-4909-ac9b-cde4ba216b7d', metadata={}, page_content='我非常喜欢去看电影。')]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对检索结果进行重新排序，根据论文的方案\n",
    "#问题相关性越低的内容块放在中间\n",
    "#问题相关性越高的内容块放在头尾\n",
    "from langchain.document_transformers import (\n",
    "    LongContextReorder\n",
    ")\n",
    "reordering = LongContextReorder()\n",
    "reo_docs = reordering.transform_documents(doc)\n",
    "\n",
    "#头尾共有4个高相关性内容块\n",
    "reo_docs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建llm\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.schema import AIMessage, HumanMessage\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    model='deepseek-chat', \n",
    "    openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "    openai_api_base='https://api.deepseek.com',\n",
    "    max_tokens=1024\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据提供的文本，没有直接提到“我不喜欢做什么”的信息。文本中只提到了“我非常喜欢去看电影”和“凯尔特人队是我最喜欢的球队”，但没有提到不喜欢的事情。因此，无法从文本中得出“我不喜欢做什么”的结论。\n"
     ]
    }
   ],
   "source": [
    "# 检测精度效果\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain,StuffDocumentsChain\n",
    "# 构建文本prompt\n",
    "doc_pormpt = PromptTemplate(input_variables=['page_content'], template='{page_content}')\n",
    "\n",
    "# 其他代码保持不变\n",
    "stuff_prompt_override = \"\"\"Given this text extracts:\n",
    "----------------------------------------\n",
    "{context}\n",
    "----------------------------------------\n",
    "Please answer the following questions:\n",
    "{query}\n",
    "\"\"\"\n",
    "\n",
    "# 定义llm_prompt\n",
    "prompt = PromptTemplate(template=stuff_prompt_override, input_variables=['context', 'query'])\n",
    "\n",
    "# 构建chain\n",
    "llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
    "\n",
    "# 构建stuff_documents_chain\n",
    "workChain = StuffDocumentsChain(llm_chain=llm_chain, document_prompt=doc_pormpt, document_variable_name=\"context\")\n",
    "\n",
    "# 构建问题\n",
    "result = workChain.run(input_documents=reo_docs, query=\"我不喜欢做什么？\")\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文本向量化 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.04128211364150047,\n",
       "  1.0518178896745667e-05,\n",
       "  -0.0013141592498868704,\n",
       "  -0.0025649401359260082,\n",
       "  -0.03253385052084923,\n",
       "  -0.020607473328709602,\n",
       "  0.008616912178695202,\n",
       "  -0.020900052040815353,\n",
       "  -0.017059646546840668,\n",
       "  -0.009424159303307533,\n",
       "  -0.002891822252422571,\n",
       "  -0.04408944770693779,\n",
       "  0.07655241340398788,\n",
       "  -0.0428006686270237,\n",
       "  -0.022342966869473457,\n",
       "  0.02974296733736992,\n",
       "  0.010820391587913036,\n",
       "  0.013455246575176716,\n",
       "  0.037555448710918427,\n",
       "  -0.05133945494890213,\n",
       "  0.0607835091650486,\n",
       "  0.021096574142575264,\n",
       "  -0.005561246536672115,\n",
       "  -0.02116318792104721,\n",
       "  0.07172644883394241,\n",
       "  -0.032687149941921234,\n",
       "  0.01073585543781519,\n",
       "  0.0321679525077343,\n",
       "  -0.01801523193717003,\n",
       "  0.016554711386561394,\n",
       "  -0.005320602562278509,\n",
       "  0.001293458859436214,\n",
       "  0.0037203682586550713,\n",
       "  0.024922387674450874,\n",
       "  -0.07037689536809921,\n",
       "  0.01590205915272236,\n",
       "  0.030190536752343178,\n",
       "  0.007778267376124859,\n",
       "  0.10584452003240585,\n",
       "  0.04517742246389389,\n",
       "  -0.028059715405106544,\n",
       "  0.038751933723688126,\n",
       "  0.01688435487449169,\n",
       "  -0.006218790542334318,\n",
       "  0.00011221269960515201,\n",
       "  0.0023575620725750923,\n",
       "  -0.0011707268422469497,\n",
       "  -0.001891139429062605,\n",
       "  -0.003271498018875718,\n",
       "  0.027682334184646606,\n",
       "  -0.03414895758032799,\n",
       "  0.05000455304980278,\n",
       "  0.03486660122871399,\n",
       "  0.007893246598541737,\n",
       "  -0.01609690487384796,\n",
       "  -0.030719980597496033,\n",
       "  0.0204385407269001,\n",
       "  -0.04882606491446495,\n",
       "  0.05404171720147133,\n",
       "  -0.003879549913108349,\n",
       "  -0.007965263910591602,\n",
       "  0.02973114885389805,\n",
       "  0.03789111226797104,\n",
       "  -0.04916497692465782,\n",
       "  0.05841066315770149,\n",
       "  -0.051772575825452805,\n",
       "  0.008464566431939602,\n",
       "  0.026039736345410347,\n",
       "  -0.0031637002248317003,\n",
       "  0.03533486649394035,\n",
       "  -0.049709610641002655,\n",
       "  0.012540089897811413,\n",
       "  0.027606043964624405,\n",
       "  0.01921788603067398,\n",
       "  -0.05294321849942207,\n",
       "  -0.008728391490876675,\n",
       "  -0.09371338039636612,\n",
       "  0.0005831170710735023,\n",
       "  -0.020964115858078003,\n",
       "  -0.01246628724038601,\n",
       "  0.042920246720314026,\n",
       "  -0.0015079976292327046,\n",
       "  0.04614920914173126,\n",
       "  -0.09747523814439774,\n",
       "  0.007116758730262518,\n",
       "  0.030836082994937897,\n",
       "  -0.013228993862867355,\n",
       "  0.0076484065502882,\n",
       "  0.013242809101939201,\n",
       "  0.03986972197890282,\n",
       "  -0.025661330670118332,\n",
       "  0.05616683140397072,\n",
       "  0.021833667531609535,\n",
       "  0.009448020718991756,\n",
       "  -0.018851183354854584,\n",
       "  0.023804819211363792,\n",
       "  0.04649864509701729,\n",
       "  0.015245761722326279,\n",
       "  -0.01431749202311039,\n",
       "  -0.056669071316719055,\n",
       "  -0.008705451153218746,\n",
       "  -0.08056933432817459,\n",
       "  0.03718813136219978,\n",
       "  -0.05679565668106079,\n",
       "  -0.03036613203585148,\n",
       "  -0.054859086871147156,\n",
       "  0.017409740015864372,\n",
       "  -0.005015023052692413,\n",
       "  0.00994046963751316,\n",
       "  0.007065466605126858,\n",
       "  -0.032138559967279434,\n",
       "  0.07410870492458344,\n",
       "  -0.024766169488430023,\n",
       "  -0.02930905483663082,\n",
       "  -0.013246466405689716,\n",
       "  0.01602308452129364,\n",
       "  -0.011650982312858105,\n",
       "  -0.0328238345682621,\n",
       "  0.02269725687801838,\n",
       "  -0.04506109282374382,\n",
       "  0.008830582723021507,\n",
       "  -0.03836951404809952,\n",
       "  0.012634333223104477,\n",
       "  0.018008437007665634,\n",
       "  0.034676697105169296,\n",
       "  0.02356928028166294,\n",
       "  0.000770804297644645,\n",
       "  0.04750317335128784,\n",
       "  0.0007915162714198232,\n",
       "  0.1014954075217247,\n",
       "  0.047025762498378754,\n",
       "  0.026928124949336052,\n",
       "  -0.04586665704846382,\n",
       "  -0.026575013995170593,\n",
       "  0.013211281038820744,\n",
       "  -0.020615415647625923,\n",
       "  0.02861836552619934,\n",
       "  0.03434509038925171,\n",
       "  -0.05129240080714226,\n",
       "  -0.05357101932168007,\n",
       "  -0.029809266328811646,\n",
       "  0.021443385630846024,\n",
       "  0.022750718519091606,\n",
       "  -0.02686910703778267,\n",
       "  0.001204284024424851,\n",
       "  -0.05360572040081024,\n",
       "  -0.009839477948844433,\n",
       "  -0.004547974094748497,\n",
       "  0.012730319052934647,\n",
       "  0.03532065078616142,\n",
       "  0.040696270763874054,\n",
       "  -0.029698163270950317,\n",
       "  -0.018231043592095375,\n",
       "  0.0068587651476264,\n",
       "  0.05390036478638649,\n",
       "  -0.011816426180303097,\n",
       "  -0.06188854202628136,\n",
       "  0.022800812497735023,\n",
       "  0.012418603524565697,\n",
       "  -0.0074771977961063385,\n",
       "  0.06700654327869415,\n",
       "  0.03733418136835098,\n",
       "  -0.026225348934531212,\n",
       "  -0.030895018950104713,\n",
       "  0.05651223286986351,\n",
       "  0.01515369862318039,\n",
       "  0.028331412002444267,\n",
       "  0.012867709621787071,\n",
       "  -0.02882310375571251,\n",
       "  -0.0862531065940857,\n",
       "  -0.007279729004949331,\n",
       "  0.0036861367989331484,\n",
       "  -0.03288215771317482,\n",
       "  0.02584126405417919,\n",
       "  -0.027643773704767227,\n",
       "  0.01799488626420498,\n",
       "  0.0034521028865128756,\n",
       "  0.05464453995227814,\n",
       "  -0.0005865656421519816,\n",
       "  -0.02701536752283573,\n",
       "  -0.03705272451043129,\n",
       "  0.006717839743942022,\n",
       "  0.04500296711921692,\n",
       "  -0.019680669531226158,\n",
       "  -0.0017911304021254182,\n",
       "  0.03486374765634537,\n",
       "  0.05075758323073387,\n",
       "  -0.03243711590766907,\n",
       "  -0.029986515641212463,\n",
       "  0.003861207515001297,\n",
       "  0.08084692060947418,\n",
       "  -0.038323093205690384,\n",
       "  0.01780485361814499,\n",
       "  -0.0443044975399971,\n",
       "  0.011284247040748596,\n",
       "  -0.02430492453277111,\n",
       "  0.022352000698447227,\n",
       "  0.03326879069209099,\n",
       "  -0.017630958929657936,\n",
       "  -0.007312677800655365,\n",
       "  -0.028712693601846695,\n",
       "  -0.030201522633433342,\n",
       "  -0.030278488993644714,\n",
       "  0.024620866402983665,\n",
       "  0.03852449730038643,\n",
       "  -0.009364251978695393,\n",
       "  0.035673875361680984,\n",
       "  0.08838395029306412,\n",
       "  0.031836215406656265,\n",
       "  -0.027363281697034836,\n",
       "  0.011120238341391087,\n",
       "  -0.04230682551860809,\n",
       "  -0.06167075037956238,\n",
       "  -0.009366356767714024,\n",
       "  0.050147440284490585,\n",
       "  0.050556205213069916,\n",
       "  -0.009464902803301811,\n",
       "  -0.07576777786016464,\n",
       "  0.0014979972038418055,\n",
       "  -0.02368325926363468,\n",
       "  0.06144675984978676,\n",
       "  -0.017062045633792877,\n",
       "  0.030278891324996948,\n",
       "  -0.04440995678305626,\n",
       "  0.002352312905713916,\n",
       "  0.03453228622674942,\n",
       "  -0.048411209136247635,\n",
       "  0.016094667837023735,\n",
       "  0.0200714822858572,\n",
       "  0.021497512236237526,\n",
       "  -0.0014911380130797625,\n",
       "  0.008292957209050655,\n",
       "  0.05806124582886696,\n",
       "  -0.0028363342862576246,\n",
       "  0.023723503574728966,\n",
       "  -0.02176285907626152,\n",
       "  -0.010012052953243256,\n",
       "  0.0077537293545901775,\n",
       "  0.015116504393517971,\n",
       "  0.003759135724976659,\n",
       "  -0.010196964256465435,\n",
       "  -0.07931307703256607,\n",
       "  0.015470366925001144,\n",
       "  0.0030376329086720943,\n",
       "  0.052729155868291855,\n",
       "  -0.02187793329358101,\n",
       "  -0.0012339423410594463,\n",
       "  0.020849086344242096,\n",
       "  -0.06751374900341034,\n",
       "  -0.009127054363489151,\n",
       "  -0.04162740334868431,\n",
       "  -0.08746471256017685,\n",
       "  -0.002554743317887187,\n",
       "  -0.009224737994372845,\n",
       "  -0.007208932191133499,\n",
       "  -0.021991727873682976,\n",
       "  -0.05343825742602348,\n",
       "  -0.038125474005937576,\n",
       "  -0.030478522181510925,\n",
       "  -0.006350074894726276,\n",
       "  -0.011644148267805576,\n",
       "  -0.021978234872221947,\n",
       "  -0.040059130638837814,\n",
       "  -0.008274246007204056,\n",
       "  -0.03858031705021858,\n",
       "  0.02520587667822838,\n",
       "  0.012197296135127544,\n",
       "  0.0310230515897274,\n",
       "  0.020246878266334534,\n",
       "  -0.043860919773578644,\n",
       "  -0.038806259632110596,\n",
       "  -0.04316091910004616,\n",
       "  0.03547174856066704,\n",
       "  0.0948389396071434,\n",
       "  0.015293044038116932,\n",
       "  -0.015987174585461617,\n",
       "  -0.04173056036233902,\n",
       "  -0.028702767565846443,\n",
       "  0.016583722084760666,\n",
       "  0.06183132156729698,\n",
       "  -0.0016757064731791615,\n",
       "  -0.009917177259922028,\n",
       "  0.04084213078022003,\n",
       "  -0.01687214896082878,\n",
       "  0.0199448112398386,\n",
       "  -0.0378628708422184,\n",
       "  -0.01092662662267685,\n",
       "  -0.01233692467212677,\n",
       "  0.07436177134513855,\n",
       "  0.05669574812054634,\n",
       "  -0.025323854759335518,\n",
       "  0.06883034110069275,\n",
       "  -0.0529736764729023,\n",
       "  -0.0006375384400598705,\n",
       "  -0.008877046406269073,\n",
       "  0.021635232493281364,\n",
       "  0.0046922145411372185,\n",
       "  -0.05892118811607361,\n",
       "  0.013018953613936901,\n",
       "  0.02343587763607502,\n",
       "  0.027436552569270134,\n",
       "  -0.018123364076018333,\n",
       "  -0.005500124767422676,\n",
       "  -0.08947644382715225,\n",
       "  -0.03803637996315956,\n",
       "  0.008831096813082695,\n",
       "  0.03720313310623169,\n",
       "  0.03541356325149536,\n",
       "  0.02069675922393799,\n",
       "  0.04092036560177803,\n",
       "  0.0006082823383621871,\n",
       "  0.03191455081105232,\n",
       "  -0.03896992653608322,\n",
       "  0.02944440394639969,\n",
       "  -0.007656400091946125,\n",
       "  0.0009971617255359888,\n",
       "  0.05688277631998062,\n",
       "  0.041231606155633926,\n",
       "  0.03553678095340729,\n",
       "  0.0022915704175829887,\n",
       "  0.014590541832149029,\n",
       "  -0.0167585089802742,\n",
       "  -0.009697012603282928,\n",
       "  0.0013596817152574658,\n",
       "  0.03904285654425621,\n",
       "  0.0436205230653286,\n",
       "  -0.0235458817332983,\n",
       "  -0.016008222475647926,\n",
       "  -0.01969919726252556,\n",
       "  -0.03255812078714371,\n",
       "  -0.0413997657597065,\n",
       "  -0.0504755973815918,\n",
       "  0.06857893615961075,\n",
       "  -0.026819489896297455,\n",
       "  -0.06213429942727089,\n",
       "  0.009683961980044842,\n",
       "  0.10556267201900482,\n",
       "  -0.03178210183978081,\n",
       "  0.09375777095556259,\n",
       "  -0.032600220292806625,\n",
       "  -0.004465815145522356,\n",
       "  0.08116348087787628,\n",
       "  0.02948318049311638,\n",
       "  0.016311101615428925,\n",
       "  -0.06231584772467613,\n",
       "  -0.004927427042275667,\n",
       "  -0.0014292077394202352,\n",
       "  -0.023747853934764862,\n",
       "  0.0025156133342534304,\n",
       "  -0.0012684942921623588,\n",
       "  0.04378992319107056,\n",
       "  0.14794491231441498,\n",
       "  0.02207266353070736,\n",
       "  0.07836025953292847,\n",
       "  0.03968856483697891,\n",
       "  -0.0165118221193552,\n",
       "  0.039465632289648056,\n",
       "  -0.023874616250395775,\n",
       "  -0.0492943674325943,\n",
       "  -0.035496070981025696,\n",
       "  -0.037354301661252975,\n",
       "  -0.021870268508791924,\n",
       "  0.016025485470891,\n",
       "  0.06536902487277985,\n",
       "  -0.02428119257092476,\n",
       "  0.023377517238259315,\n",
       "  0.006178849842399359,\n",
       "  -0.012239810079336166,\n",
       "  -0.0447772778570652,\n",
       "  -0.03525542467832565,\n",
       "  0.033948712050914764,\n",
       "  -0.0028672246262431145,\n",
       "  0.048492878675460815,\n",
       "  -0.008755490183830261,\n",
       "  -0.025172432884573936,\n",
       "  -0.013137188740074635,\n",
       "  -0.020513193681836128,\n",
       "  0.003516792319715023,\n",
       "  -0.03203583508729935,\n",
       "  0.04409678652882576,\n",
       "  0.01549307070672512,\n",
       "  -0.004593955352902412,\n",
       "  -0.0046665240079164505,\n",
       "  -0.004188150633126497,\n",
       "  -0.003647975856438279,\n",
       "  0.031697750091552734,\n",
       "  0.01969527266919613,\n",
       "  0.0093194879591465,\n",
       "  0.028674039989709854,\n",
       "  -0.014412703923881054,\n",
       "  -0.0382162369787693,\n",
       "  0.04321419447660446,\n",
       "  0.0019860302563756704,\n",
       "  -0.05436709150671959,\n",
       "  0.03823750466108322,\n",
       "  0.04353580251336098,\n",
       "  -0.006973929703235626,\n",
       "  0.02908160537481308,\n",
       "  0.013068403117358685,\n",
       "  -0.07075825333595276,\n",
       "  0.04677426815032959,\n",
       "  -0.04659785330295563,\n",
       "  -0.09850876033306122,\n",
       "  -0.022444378584623337,\n",
       "  0.022846227511763573,\n",
       "  -0.022099409252405167,\n",
       "  -0.01963207684457302,\n",
       "  0.04731433466076851,\n",
       "  -0.014259548857808113,\n",
       "  -0.025805573910474777,\n",
       "  0.011604202911257744,\n",
       "  0.033025726675987244,\n",
       "  -0.011608708649873734,\n",
       "  0.03259417042136192,\n",
       "  -0.044899627566337585,\n",
       "  -0.03239409998059273,\n",
       "  0.004047939088195562,\n",
       "  0.01667378842830658,\n",
       "  -0.06444968283176422,\n",
       "  0.005823822226375341,\n",
       "  -0.04354667663574219,\n",
       "  0.03368617594242096,\n",
       "  0.0225064679980278,\n",
       "  0.020822977647185326,\n",
       "  -0.021359039470553398,\n",
       "  0.004436008166521788,\n",
       "  -0.0020685812924057245,\n",
       "  0.004482683259993792,\n",
       "  0.03398781642317772,\n",
       "  -0.029895901679992676,\n",
       "  0.02816050313413143,\n",
       "  -0.0046044886112213135,\n",
       "  -0.03720657899975777,\n",
       "  -0.01819709688425064,\n",
       "  -0.057457972317934036,\n",
       "  -0.04755053296685219,\n",
       "  0.031102193519473076,\n",
       "  -0.03164827451109886,\n",
       "  -0.042983297258615494,\n",
       "  0.03618824854493141,\n",
       "  -0.00043013671529479325,\n",
       "  0.036200083792209625,\n",
       "  0.00472842575982213,\n",
       "  0.006497996859252453,\n",
       "  -0.024513058364391327,\n",
       "  0.011423580348491669,\n",
       "  -0.0060198609717190266,\n",
       "  0.042826708406209946,\n",
       "  0.05441482365131378,\n",
       "  -0.04113943874835968,\n",
       "  0.008471865206956863,\n",
       "  0.01537977997213602,\n",
       "  0.005997988861054182,\n",
       "  0.013238957151770592,\n",
       "  0.002929210662841797,\n",
       "  -0.05472908914089203,\n",
       "  0.07524489611387253,\n",
       "  -0.06675835698843002,\n",
       "  -0.02113437093794346,\n",
       "  0.01063775084912777,\n",
       "  0.05266721919178963,\n",
       "  -0.06481677293777466,\n",
       "  -0.09436514973640442,\n",
       "  -0.02669605426490307,\n",
       "  -0.02423238381743431,\n",
       "  0.023331332951784134,\n",
       "  0.008955295197665691,\n",
       "  0.0245315320789814,\n",
       "  0.0038935362827032804,\n",
       "  0.0159988421946764,\n",
       "  -0.0014383071102201939,\n",
       "  -0.025020768865942955,\n",
       "  0.01824375055730343,\n",
       "  -0.07125715911388397,\n",
       "  -0.06377670168876648,\n",
       "  0.029799478128552437,\n",
       "  -0.011712903156876564,\n",
       "  0.052656516432762146,\n",
       "  -0.050397057086229324,\n",
       "  -0.001385642564855516,\n",
       "  0.051617130637168884,\n",
       "  -0.014190413989126682,\n",
       "  -0.04107610881328583,\n",
       "  0.04642447829246521,\n",
       "  -0.03607747703790665,\n",
       "  0.05478915199637413,\n",
       "  -0.02328951098024845,\n",
       "  -0.03482302650809288,\n",
       "  -0.018538635224103928,\n",
       "  -0.0588090643286705,\n",
       "  0.10504350811243057,\n",
       "  -0.030897794291377068,\n",
       "  0.01209341548383236,\n",
       "  -0.003822578117251396,\n",
       "  -0.004098708741366863,\n",
       "  0.04505227133631706,\n",
       "  -0.011513186618685722,\n",
       "  0.016428964212536812,\n",
       "  -0.0002959470439236611,\n",
       "  0.0048640333116054535,\n",
       "  -0.030898617580533028,\n",
       "  -0.011425540782511234,\n",
       "  0.007489046081900597,\n",
       "  0.019205721095204353,\n",
       "  -0.04861212521791458,\n",
       "  0.032397814095020294,\n",
       "  0.013982580043375492,\n",
       "  -0.015350578352808952,\n",
       "  -0.014715226367115974,\n",
       "  0.013386082835495472,\n",
       "  0.023645389825105667,\n",
       "  -0.047020453959703445,\n",
       "  0.006304461043328047,\n",
       "  -0.021538974717259407,\n",
       "  0.06800564378499985,\n",
       "  0.033234853297472,\n",
       "  0.05505656450986862,\n",
       "  -0.03495548665523529,\n",
       "  -0.001324100885540247,\n",
       "  -0.04750264063477516,\n",
       "  -0.02420498989522457,\n",
       "  -0.03608327358961105,\n",
       "  0.03292235732078552,\n",
       "  0.023603348061442375,\n",
       "  -0.05906232073903084,\n",
       "  -0.037824004888534546,\n",
       "  0.014248555526137352,\n",
       "  -0.064740851521492,\n",
       "  0.013463886454701424,\n",
       "  -0.034678634256124496,\n",
       "  -0.03545435890555382,\n",
       "  0.04837002605199814,\n",
       "  -0.039406124502420425,\n",
       "  -0.012250551953911781,\n",
       "  -0.007994509302079678,\n",
       "  -0.03318805992603302,\n",
       "  0.017398905009031296,\n",
       "  0.012470570392906666,\n",
       "  -0.025321079418063164,\n",
       "  -0.01130308210849762,\n",
       "  0.012327531352639198,\n",
       "  -0.0032309112139046192,\n",
       "  0.050124794244766235,\n",
       "  -0.020611794665455818,\n",
       "  0.011170117184519768,\n",
       "  -0.0012084435438737273,\n",
       "  -0.03790513426065445,\n",
       "  0.0023496143985539675,\n",
       "  0.017587989568710327,\n",
       "  0.012759646400809288,\n",
       "  -0.011851373128592968,\n",
       "  -0.07594376802444458,\n",
       "  -0.03393290936946869,\n",
       "  -0.003425138769671321,\n",
       "  -0.01392234768718481,\n",
       "  0.07499054819345474,\n",
       "  0.003578320611268282,\n",
       "  -0.04004770144820213,\n",
       "  -0.027479687705636024,\n",
       "  -0.012423359788954258,\n",
       "  0.006195213180035353,\n",
       "  0.06827200949192047,\n",
       "  -0.0393511988222599,\n",
       "  -0.030594931915402412,\n",
       "  0.025960702449083328,\n",
       "  -0.031001735478639603,\n",
       "  0.013641840778291225,\n",
       "  0.025573808699846268,\n",
       "  -0.033716797828674316,\n",
       "  0.07406415790319443,\n",
       "  -0.05306244269013405,\n",
       "  0.03997788950800896,\n",
       "  -0.02429301291704178,\n",
       "  0.034072715789079666,\n",
       "  0.09090504795312881,\n",
       "  0.008704977110028267,\n",
       "  0.01168802659958601,\n",
       "  -0.008000540547072887,\n",
       "  -0.023719634860754013,\n",
       "  -0.04056016355752945,\n",
       "  0.05561256408691406,\n",
       "  0.059799499809741974,\n",
       "  0.040693141520023346,\n",
       "  0.016377607360482216,\n",
       "  -0.04802800342440605,\n",
       "  0.00826474092900753,\n",
       "  0.015050061047077179,\n",
       "  0.01341024599969387,\n",
       "  -0.12294057756662369,\n",
       "  0.08426328003406525,\n",
       "  -0.019956136122345924,\n",
       "  -0.006562878377735615,\n",
       "  0.07991446554660797,\n",
       "  -0.030694326385855675,\n",
       "  0.0343039445579052,\n",
       "  0.04440535977482796,\n",
       "  0.017610162496566772,\n",
       "  -0.006751505192369223,\n",
       "  0.02134881727397442,\n",
       "  -0.07003367692232132,\n",
       "  0.047846194356679916,\n",
       "  0.008979435078799725,\n",
       "  -0.0024998353328555822,\n",
       "  0.034632761031389236,\n",
       "  0.026826852932572365,\n",
       "  0.023685220628976822,\n",
       "  0.04085443168878555,\n",
       "  0.005124017130583525,\n",
       "  0.000805794436018914,\n",
       "  -0.037403710186481476,\n",
       "  -0.008407912217080593,\n",
       "  -0.01656971126794815,\n",
       "  0.010382608510553837,\n",
       "  0.047826576977968216,\n",
       "  0.04210352525115013,\n",
       "  -0.051994647830724716,\n",
       "  -0.02298349142074585,\n",
       "  -0.01590149849653244,\n",
       "  -0.009130319580435753,\n",
       "  -0.03887660801410675,\n",
       "  0.013052030466496944,\n",
       "  -0.007338488474488258,\n",
       "  -0.005744278430938721,\n",
       "  -0.042036283761262894,\n",
       "  0.019219160079956055,\n",
       "  0.022208411246538162,\n",
       "  0.0014679167652502656,\n",
       "  0.043986976146698,\n",
       "  -0.040883321315050125,\n",
       "  0.020580604672431946,\n",
       "  -0.02155531570315361,\n",
       "  -0.017044343054294586,\n",
       "  -0.0014927710872143507,\n",
       "  0.02784550003707409,\n",
       "  -0.02295597270131111,\n",
       "  -0.02042292058467865,\n",
       "  0.0005876600043848157,\n",
       "  -0.013612666167318821,\n",
       "  0.004261612892150879,\n",
       "  0.007449892815202475,\n",
       "  -0.04526873305439949,\n",
       "  -0.04112577065825462,\n",
       "  0.03255753219127655,\n",
       "  0.057173486799001694,\n",
       "  0.029629724100232124,\n",
       "  0.038628753274679184,\n",
       "  -0.035356663167476654,\n",
       "  -0.045786820352077484,\n",
       "  0.044579487293958664,\n",
       "  0.056507933884859085,\n",
       "  -0.02398187667131424,\n",
       "  -0.026806754991412163,\n",
       "  -0.002298625186085701,\n",
       "  0.024077514186501503,\n",
       "  0.034178223460912704,\n",
       "  -0.004267374984920025,\n",
       "  -0.03365055471658707,\n",
       "  0.050403427332639694,\n",
       "  0.027691271156072617,\n",
       "  0.03478456661105156,\n",
       "  -0.02055177465081215,\n",
       "  0.029682772234082222,\n",
       "  -0.018970487639307976,\n",
       "  -0.01464810874313116,\n",
       "  0.008806546218693256,\n",
       "  0.029790766537189484,\n",
       "  -0.0005637638387270272,\n",
       "  0.01336491759866476,\n",
       "  0.0028295759111642838,\n",
       "  -0.0916578620672226,\n",
       "  -0.003477419726550579,\n",
       "  0.01749853417277336,\n",
       "  -0.031109286472201347,\n",
       "  0.022697966545820236,\n",
       "  0.023569276556372643,\n",
       "  0.028269121423363686,\n",
       "  0.04285404086112976,\n",
       "  0.04079939052462578,\n",
       "  0.012436874210834503,\n",
       "  -0.030376454815268517,\n",
       "  0.01675332896411419,\n",
       "  0.0024522515013813972,\n",
       "  0.0014553581131622195,\n",
       "  0.0024831360206007957,\n",
       "  0.04235975444316864,\n",
       "  0.05284908786416054,\n",
       "  -0.03307078033685684,\n",
       "  0.030536457896232605,\n",
       "  -0.03511754050850868,\n",
       "  -0.034951020032167435,\n",
       "  -0.0007698411936871707,\n",
       "  -0.007367121521383524,\n",
       "  0.06398946046829224,\n",
       "  -0.015914028510451317,\n",
       "  -0.014701024629175663,\n",
       "  0.007194242440164089,\n",
       "  0.04224091023206711,\n",
       "  0.01520019955933094,\n",
       "  0.01732240989804268,\n",
       "  0.02268119715154171,\n",
       "  0.0020197806879878044,\n",
       "  0.012152311392128468,\n",
       "  0.0309639610350132,\n",
       "  -0.0196931604295969,\n",
       "  0.041478559374809265,\n",
       "  -0.07860950380563736,\n",
       "  -0.05547948554158211,\n",
       "  -0.03357116878032684,\n",
       "  -0.005416507832705975,\n",
       "  0.055516812950372696,\n",
       "  0.002687753178179264,\n",
       "  -0.05822725594043732,\n",
       "  0.05682151019573212,\n",
       "  -0.02227679267525673,\n",
       "  0.016848281025886536,\n",
       "  -0.010113807395100594,\n",
       "  0.04968997463583946,\n",
       "  0.06819073110818863,\n",
       "  -0.024253495037555695,\n",
       "  0.03227325528860092,\n",
       "  0.04177165776491165,\n",
       "  -0.0017274732235819101,\n",
       "  0.017523277550935745,\n",
       "  0.053093791007995605,\n",
       "  0.02138189785182476,\n",
       "  -0.006329040043056011,\n",
       "  0.030882015824317932,\n",
       "  0.026245122775435448,\n",
       "  0.03430134803056717,\n",
       "  0.002291622571647167,\n",
       "  0.037592604756355286,\n",
       "  0.018621880561113358,\n",
       "  -0.009500063955783844,\n",
       "  0.017395317554473877,\n",
       "  -0.0018658937187865376,\n",
       "  0.006655095610767603,\n",
       "  -0.048241596668958664,\n",
       "  0.00707184337079525,\n",
       "  0.007552826311439276,\n",
       "  -0.0372648648917675,\n",
       "  -0.04255853220820427,\n",
       "  -0.037277087569236755,\n",
       "  0.023124000057578087,\n",
       "  0.013860257342457771,\n",
       "  0.002381414407864213,\n",
       "  -0.01188606210052967,\n",
       "  -0.050097230821847916,\n",
       "  -0.00041710096411406994,\n",
       "  0.003047489793971181,\n",
       "  0.040966812521219254,\n",
       "  -0.012451558373868465,\n",
       "  0.03171943128108978,\n",
       "  -0.027879079803824425,\n",
       "  -0.07239613682031631,\n",
       "  0.06561566889286041,\n",
       "  0.033015377819538116,\n",
       "  -0.014308847486972809,\n",
       "  -0.029435541480779648,\n",
       "  -0.0002882711123675108,\n",
       "  -0.026825519278645515,\n",
       "  0.00676968228071928,\n",
       "  -0.061149656772613525,\n",
       "  -0.005282777361571789,\n",
       "  -0.010495378635823727,\n",
       "  0.05036069452762604,\n",
       "  -0.09959258139133453,\n",
       "  -0.011255642399191856,\n",
       "  -0.008741707541048527],\n",
       " [0.029881883412599564,\n",
       "  0.02279316633939743,\n",
       "  -0.007646488957107067,\n",
       "  0.013337668962776661,\n",
       "  -0.024452341720461845,\n",
       "  -0.023326117545366287,\n",
       "  0.022731173783540726,\n",
       "  -0.0070463502779603004,\n",
       "  -0.00800391472876072,\n",
       "  -0.012929413467645645,\n",
       "  -0.046054691076278687,\n",
       "  -0.04372308403253555,\n",
       "  0.11206714063882828,\n",
       "  -0.05303748697042465,\n",
       "  -0.02536078542470932,\n",
       "  0.019807567819952965,\n",
       "  0.015137570910155773,\n",
       "  -0.026887863874435425,\n",
       "  0.019148482009768486,\n",
       "  -0.0392523854970932,\n",
       "  0.07253018766641617,\n",
       "  0.011477423831820488,\n",
       "  0.001087495475076139,\n",
       "  -0.002035287907347083,\n",
       "  0.06396635621786118,\n",
       "  -0.008604461327195168,\n",
       "  0.030262574553489685,\n",
       "  0.04291920363903046,\n",
       "  -0.04568164050579071,\n",
       "  0.007009753491729498,\n",
       "  -0.024806035682559013,\n",
       "  0.008598770946264267,\n",
       "  0.0010486964602023363,\n",
       "  -0.015930194407701492,\n",
       "  -0.06975895166397095,\n",
       "  0.03277520462870598,\n",
       "  0.013554291799664497,\n",
       "  0.00377696193754673,\n",
       "  0.12077157944440842,\n",
       "  0.044408999383449554,\n",
       "  -0.026681531220674515,\n",
       "  0.026965109631419182,\n",
       "  -0.013083242811262608,\n",
       "  -0.034288763999938965,\n",
       "  -0.010504564270377159,\n",
       "  -0.003945870324969292,\n",
       "  0.007517552934587002,\n",
       "  0.0045710364356637,\n",
       "  0.01697099581360817,\n",
       "  0.0260755717754364,\n",
       "  -0.02246674709022045,\n",
       "  0.03590066730976105,\n",
       "  0.0006488548824563622,\n",
       "  0.00866981502622366,\n",
       "  -0.02501913718879223,\n",
       "  -0.054958708584308624,\n",
       "  -0.005973220337182283,\n",
       "  -0.05228119343519211,\n",
       "  0.06514182686805725,\n",
       "  -0.021763475611805916,\n",
       "  -0.018106045201420784,\n",
       "  0.010161763988435268,\n",
       "  0.022314943373203278,\n",
       "  -0.04082727059721947,\n",
       "  0.08408230543136597,\n",
       "  -0.0347064845263958,\n",
       "  0.0058962516486644745,\n",
       "  0.018093738704919815,\n",
       "  0.021530408412218094,\n",
       "  0.03921797126531601,\n",
       "  -0.044891852885484695,\n",
       "  0.018603399395942688,\n",
       "  0.006065449211746454,\n",
       "  0.017578687518835068,\n",
       "  -0.043681252747774124,\n",
       "  -0.0037365444004535675,\n",
       "  -0.08223949372768402,\n",
       "  0.002949373098090291,\n",
       "  -0.013329314067959785,\n",
       "  0.0067426241002976894,\n",
       "  0.03671017661690712,\n",
       "  -0.02889612689614296,\n",
       "  0.02526242285966873,\n",
       "  -0.08509378880262375,\n",
       "  -0.011782346293330193,\n",
       "  0.035199008882045746,\n",
       "  -0.0071447622030973434,\n",
       "  0.015131049789488316,\n",
       "  0.020398743450641632,\n",
       "  0.007204156368970871,\n",
       "  -0.02064967341721058,\n",
       "  0.04266735166311264,\n",
       "  0.012925948016345501,\n",
       "  -9.585079533280805e-05,\n",
       "  -0.004734937567263842,\n",
       "  0.02619248814880848,\n",
       "  0.06968524307012558,\n",
       "  0.005853015463799238,\n",
       "  -0.015801778063178062,\n",
       "  -0.07758818566799164,\n",
       "  0.0026784902438521385,\n",
       "  -0.07011865079402924,\n",
       "  0.04337054118514061,\n",
       "  -0.04200093075633049,\n",
       "  0.0152768325060606,\n",
       "  -0.040008772164583206,\n",
       "  0.0004445148224476725,\n",
       "  -0.0022154408507049084,\n",
       "  0.02018743008375168,\n",
       "  -0.011074508540332317,\n",
       "  -0.042177245020866394,\n",
       "  0.07685939967632294,\n",
       "  -0.026430746540427208,\n",
       "  -0.0274345763027668,\n",
       "  -0.060186583548784256,\n",
       "  -0.010826818645000458,\n",
       "  0.014678684994578362,\n",
       "  -0.03995508328080177,\n",
       "  0.025028418749570847,\n",
       "  -0.06003351882100105,\n",
       "  0.016977766528725624,\n",
       "  -0.024698715656995773,\n",
       "  0.018616661429405212,\n",
       "  0.0175485759973526,\n",
       "  0.03854359686374664,\n",
       "  0.009642048738896847,\n",
       "  0.005199380684643984,\n",
       "  0.028252923861145973,\n",
       "  -0.02266130968928337,\n",
       "  0.07069262862205505,\n",
       "  0.02030494064092636,\n",
       "  0.00170612963847816,\n",
       "  -0.04740054905414581,\n",
       "  -0.01639876514673233,\n",
       "  -0.007241205312311649,\n",
       "  -0.010715901851654053,\n",
       "  0.02706712856888771,\n",
       "  0.024044496938586235,\n",
       "  -0.04472110792994499,\n",
       "  -0.060279522091150284,\n",
       "  -0.009478593245148659,\n",
       "  0.0158982090651989,\n",
       "  0.029609326273202896,\n",
       "  -0.02140948921442032,\n",
       "  0.010922406800091267,\n",
       "  -0.04963821545243263,\n",
       "  0.03614462912082672,\n",
       "  -0.018648726865649223,\n",
       "  0.021951807662844658,\n",
       "  0.04684038832783699,\n",
       "  0.03343956172466278,\n",
       "  -0.05156976729631424,\n",
       "  -0.002101575955748558,\n",
       "  0.015638025477528572,\n",
       "  0.047614894807338715,\n",
       "  -0.018887141719460487,\n",
       "  0.007118659093976021,\n",
       "  0.019347479566931725,\n",
       "  -0.01051407866179943,\n",
       "  -0.001999122556298971,\n",
       "  0.06671386957168579,\n",
       "  0.025965817272663116,\n",
       "  -0.0323246568441391,\n",
       "  -0.03687635064125061,\n",
       "  0.05639203265309334,\n",
       "  0.017075849696993828,\n",
       "  0.01851343736052513,\n",
       "  0.030732575803995132,\n",
       "  -0.033209819346666336,\n",
       "  -0.07830418646335602,\n",
       "  0.003154580481350422,\n",
       "  0.012091638520359993,\n",
       "  -0.03907336667180061,\n",
       "  0.007076927926391363,\n",
       "  -0.021470829844474792,\n",
       "  0.010027989745140076,\n",
       "  -0.019478689879179,\n",
       "  0.04336734861135483,\n",
       "  -0.0006091927061788738,\n",
       "  -0.04210791364312172,\n",
       "  -0.028373733162879944,\n",
       "  0.0021447602193802595,\n",
       "  0.0688958466053009,\n",
       "  -0.0025810962542891502,\n",
       "  0.007861181162297726,\n",
       "  0.0327746756374836,\n",
       "  0.03705481067299843,\n",
       "  -0.030959073454141617,\n",
       "  -0.029929686337709427,\n",
       "  0.020628584548830986,\n",
       "  0.0348324291408062,\n",
       "  -0.014588004909455776,\n",
       "  0.03518524765968323,\n",
       "  -0.0476265624165535,\n",
       "  0.0336490198969841,\n",
       "  -0.04874948412179947,\n",
       "  0.050573382526636124,\n",
       "  0.002537781372666359,\n",
       "  -0.004526239354163408,\n",
       "  -0.0030364517588168383,\n",
       "  -0.01696329563856125,\n",
       "  0.0026484988629817963,\n",
       "  -0.049758877605199814,\n",
       "  -0.0020486339926719666,\n",
       "  0.02343318797647953,\n",
       "  -0.020749462768435478,\n",
       "  0.04964146018028259,\n",
       "  0.09460444748401642,\n",
       "  0.0262195635586977,\n",
       "  -0.006153314374387264,\n",
       "  0.020178459584712982,\n",
       "  -0.03609080612659454,\n",
       "  -0.0555255226790905,\n",
       "  -0.012201706878840923,\n",
       "  0.052905041724443436,\n",
       "  0.03778280317783356,\n",
       "  -0.008329066447913647,\n",
       "  -0.09371695667505264,\n",
       "  0.010335695929825306,\n",
       "  -0.01121299248188734,\n",
       "  0.04299858957529068,\n",
       "  -0.006665761116892099,\n",
       "  0.04371488839387894,\n",
       "  -0.02916235290467739,\n",
       "  0.009840695187449455,\n",
       "  0.0563131719827652,\n",
       "  -0.015844091773033142,\n",
       "  0.013769177719950676,\n",
       "  0.031294044107198715,\n",
       "  -0.02345079369843006,\n",
       "  -0.0020271807443350554,\n",
       "  0.026294363662600517,\n",
       "  0.052113115787506104,\n",
       "  0.023228097707033157,\n",
       "  0.0031556114554405212,\n",
       "  0.005910694133490324,\n",
       "  0.006949540227651596,\n",
       "  -0.012280985713005066,\n",
       "  0.0018245127284899354,\n",
       "  -0.009523040615022182,\n",
       "  0.013226707465946674,\n",
       "  -0.061672646552324295,\n",
       "  0.014674818143248558,\n",
       "  -0.0004827390075661242,\n",
       "  0.07093531638383865,\n",
       "  0.006066865287721157,\n",
       "  0.008120782673358917,\n",
       "  0.02519327960908413,\n",
       "  -0.03933187574148178,\n",
       "  0.017578888684511185,\n",
       "  -0.026651663705706596,\n",
       "  -0.045414038002491,\n",
       "  0.008245148696005344,\n",
       "  0.0239598136395216,\n",
       "  -0.005909218452870846,\n",
       "  -0.007671951316297054,\n",
       "  -0.05882132798433304,\n",
       "  -0.02717560902237892,\n",
       "  0.0012968566734343767,\n",
       "  -0.002493875101208687,\n",
       "  -0.0037525794468820095,\n",
       "  -0.0261540450155735,\n",
       "  -0.045499980449676514,\n",
       "  0.0011813562596216798,\n",
       "  -0.034167468547821045,\n",
       "  0.036576684564352036,\n",
       "  0.017555540427565575,\n",
       "  0.034607186913490295,\n",
       "  0.010321143083274364,\n",
       "  -0.018425477668642998,\n",
       "  -0.01795026659965515,\n",
       "  -0.044132765382528305,\n",
       "  0.033098477870225906,\n",
       "  0.11643918603658676,\n",
       "  0.03728457912802696,\n",
       "  -0.009595295414328575,\n",
       "  -0.010601705871522427,\n",
       "  0.025089023634791374,\n",
       "  0.059432629495859146,\n",
       "  0.017818443477153778,\n",
       "  -0.021457325667142868,\n",
       "  -0.04146312177181244,\n",
       "  0.022888896986842155,\n",
       "  -0.034136343747377396,\n",
       "  0.030139993876218796,\n",
       "  -0.02429250068962574,\n",
       "  -0.02528342604637146,\n",
       "  -0.02312394790351391,\n",
       "  0.0662250816822052,\n",
       "  0.02359820157289505,\n",
       "  -0.024439293891191483,\n",
       "  0.08672471344470978,\n",
       "  -0.037546947598457336,\n",
       "  -0.03016940876841545,\n",
       "  -0.013964767567813396,\n",
       "  0.02475733309984207,\n",
       "  0.002883440814912319,\n",
       "  -0.07282517850399017,\n",
       "  0.0010833872947841883,\n",
       "  0.030833525583148003,\n",
       "  0.03328825160861015,\n",
       "  -0.024713991209864616,\n",
       "  -0.0045550004579126835,\n",
       "  -0.06980608403682709,\n",
       "  -0.06436314433813095,\n",
       "  -0.02013523317873478,\n",
       "  0.05227057635784149,\n",
       "  0.022235002368688583,\n",
       "  0.0004257633991073817,\n",
       "  0.03398779407143593,\n",
       "  -0.020010357722640038,\n",
       "  0.0354154072701931,\n",
       "  -0.057437293231487274,\n",
       "  0.052445150911808014,\n",
       "  0.012268821708858013,\n",
       "  0.0006119902245700359,\n",
       "  0.049576036632061005,\n",
       "  0.04367642104625702,\n",
       "  0.05655525252223015,\n",
       "  0.02848215587437153,\n",
       "  0.00598248653113842,\n",
       "  -0.005900123156607151,\n",
       "  -0.009953582659363747,\n",
       "  -0.023825008422136307,\n",
       "  0.019665054976940155,\n",
       "  0.045367687940597534,\n",
       "  0.002270434983074665,\n",
       "  -0.007935156114399433,\n",
       "  -0.0001882181386463344,\n",
       "  -0.01658773235976696,\n",
       "  -0.020145148038864136,\n",
       "  -0.05156472325325012,\n",
       "  0.049258239567279816,\n",
       "  -0.0033660298213362694,\n",
       "  -0.050389613956213,\n",
       "  0.016541874036192894,\n",
       "  0.08603233844041824,\n",
       "  -0.013279222883284092,\n",
       "  0.06484540551900864,\n",
       "  -0.024468576535582542,\n",
       "  -0.043834101408720016,\n",
       "  0.06817504018545151,\n",
       "  0.048450715839862823,\n",
       "  -0.004336367826908827,\n",
       "  -0.0467308908700943,\n",
       "  -0.018394997343420982,\n",
       "  0.011733626946806908,\n",
       "  -0.005001421552151442,\n",
       "  0.028872448951005936,\n",
       "  -0.006111847702413797,\n",
       "  0.04793141782283783,\n",
       "  0.11006440222263336,\n",
       "  0.03213728219270706,\n",
       "  0.07387152314186096,\n",
       "  0.04944542050361633,\n",
       "  -0.05181043595075607,\n",
       "  0.05682796612381935,\n",
       "  -0.044614437967538834,\n",
       "  -0.042058899998664856,\n",
       "  -0.02715180814266205,\n",
       "  -0.04857346788048744,\n",
       "  -0.03605823591351509,\n",
       "  0.011625494807958603,\n",
       "  0.04534710943698883,\n",
       "  -0.040146056562662125,\n",
       "  0.024189602583646774,\n",
       "  0.04416109248995781,\n",
       "  0.0027614261489361525,\n",
       "  -0.02204093337059021,\n",
       "  -0.005022092722356319,\n",
       "  0.008134531788527966,\n",
       "  -0.0021662332583218813,\n",
       "  0.04007716849446297,\n",
       "  -0.009595976211130619,\n",
       "  -0.013685338199138641,\n",
       "  -0.0055613163858652115,\n",
       "  -0.0005897349328733981,\n",
       "  0.006371839437633753,\n",
       "  -0.05320237949490547,\n",
       "  0.056267429143190384,\n",
       "  0.04455438628792763,\n",
       "  -0.020117394626140594,\n",
       "  -0.02103728987276554,\n",
       "  0.0002022904227487743,\n",
       "  0.039469532668590546,\n",
       "  0.006412825547158718,\n",
       "  0.03135504201054573,\n",
       "  0.007452011574059725,\n",
       "  0.048701509833335876,\n",
       "  -0.003916310146450996,\n",
       "  -0.04032992571592331,\n",
       "  0.06062626093626022,\n",
       "  0.0023890631273388863,\n",
       "  -0.06655271351337433,\n",
       "  0.026017839089035988,\n",
       "  0.05956532806158066,\n",
       "  -0.019742848351597786,\n",
       "  0.03684438019990921,\n",
       "  -0.008372928015887737,\n",
       "  -0.08604642748832703,\n",
       "  0.026386966928839684,\n",
       "  -0.02585604228079319,\n",
       "  -0.10134392231702805,\n",
       "  -0.01691373810172081,\n",
       "  0.03332541137933731,\n",
       "  -0.0336126945912838,\n",
       "  -0.014423105865716934,\n",
       "  0.022092560306191444,\n",
       "  -0.0032772747799754143,\n",
       "  -0.04031748324632645,\n",
       "  -0.005742124747484922,\n",
       "  0.010881857015192509,\n",
       "  -0.004638404119759798,\n",
       "  0.0689067393541336,\n",
       "  -0.024949857965111732,\n",
       "  -0.02522485889494419,\n",
       "  -0.00763298524543643,\n",
       "  0.018142737448215485,\n",
       "  -0.055109649896621704,\n",
       "  0.0021203767973929644,\n",
       "  -0.0472177118062973,\n",
       "  0.04879024252295494,\n",
       "  0.01814303733408451,\n",
       "  0.0011185419280081987,\n",
       "  -0.03809557110071182,\n",
       "  -0.025275127962231636,\n",
       "  -0.012706623412668705,\n",
       "  0.027244823053479195,\n",
       "  0.0021989985834807158,\n",
       "  -0.0312991663813591,\n",
       "  0.010494116693735123,\n",
       "  -0.004766205325722694,\n",
       "  -0.028706055134534836,\n",
       "  -0.05301332101225853,\n",
       "  -0.019090045243501663,\n",
       "  -0.041577089577913284,\n",
       "  0.016620660200715065,\n",
       "  -0.02486041747033596,\n",
       "  -0.032446496188640594,\n",
       "  0.027175258845090866,\n",
       "  -0.02003621496260166,\n",
       "  0.03397407755255699,\n",
       "  0.0011908120941370726,\n",
       "  -0.001390076125971973,\n",
       "  -0.03028740920126438,\n",
       "  0.0027318792417645454,\n",
       "  0.017606819048523903,\n",
       "  -0.00037480550236068666,\n",
       "  0.03694974258542061,\n",
       "  -0.047909535467624664,\n",
       "  0.01830512285232544,\n",
       "  0.032265350222587585,\n",
       "  0.004885608796030283,\n",
       "  0.006155682727694511,\n",
       "  -0.009405971504747868,\n",
       "  -0.05272190645337105,\n",
       "  0.05597763881087303,\n",
       "  -0.049540311098098755,\n",
       "  -0.01479277666658163,\n",
       "  0.01674455963075161,\n",
       "  0.02498128078877926,\n",
       "  -0.021827246993780136,\n",
       "  -0.07938908040523529,\n",
       "  -0.035366348922252655,\n",
       "  -0.010676009580492973,\n",
       "  0.04560660943388939,\n",
       "  0.012565046548843384,\n",
       "  0.026119370013475418,\n",
       "  0.0157605092972517,\n",
       "  0.025722553953528404,\n",
       "  0.0015418723924085498,\n",
       "  -0.05048698931932449,\n",
       "  0.015592367388308048,\n",
       "  -0.07491288334131241,\n",
       "  -0.06149798631668091,\n",
       "  0.01958933100104332,\n",
       "  -0.005412987899035215,\n",
       "  0.05034705996513367,\n",
       "  -0.04338836297392845,\n",
       "  -0.03947901725769043,\n",
       "  0.05475877597928047,\n",
       "  -0.01624915562570095,\n",
       "  -0.05339498817920685,\n",
       "  0.03829406946897507,\n",
       "  -0.026005791500210762,\n",
       "  0.04973863437771797,\n",
       "  -0.04100847244262695,\n",
       "  0.006357335951179266,\n",
       "  0.007554687559604645,\n",
       "  -0.08176875859498978,\n",
       "  0.0709739550948143,\n",
       "  -0.009240925312042236,\n",
       "  0.030603595077991486,\n",
       "  -0.0246394332498312,\n",
       "  -0.03679753839969635,\n",
       "  0.03265707567334175,\n",
       "  0.0065381936728954315,\n",
       "  -0.006463336758315563,\n",
       "  0.03541705757379532,\n",
       "  -0.0025884907227009535,\n",
       "  -0.034957434982061386,\n",
       "  0.004934340249747038,\n",
       "  -0.006965970620512962,\n",
       "  0.010744545608758926,\n",
       "  -0.09670405089855194,\n",
       "  0.03514546528458595,\n",
       "  0.00698276748880744,\n",
       "  -0.022732611745595932,\n",
       "  -0.009893870912492275,\n",
       "  0.013048117980360985,\n",
       "  -0.023663785308599472,\n",
       "  -0.044176165014505386,\n",
       "  0.033580150455236435,\n",
       "  -0.0015144196804612875,\n",
       "  0.06667686253786087,\n",
       "  0.03417133167386055,\n",
       "  0.037591349333524704,\n",
       "  -0.02418736182153225,\n",
       "  0.016757536679506302,\n",
       "  -0.05195978656411171,\n",
       "  -0.061574745923280716,\n",
       "  -0.06173558533191681,\n",
       "  0.00866101123392582,\n",
       "  0.01071491464972496,\n",
       "  -0.03931966796517372,\n",
       "  -0.04408365115523338,\n",
       "  0.006718836724758148,\n",
       "  -0.03868163377046585,\n",
       "  -0.004455921705812216,\n",
       "  -0.034985024482011795,\n",
       "  -0.0348946638405323,\n",
       "  0.02666160836815834,\n",
       "  -0.04712493717670441,\n",
       "  -0.0304579958319664,\n",
       "  -0.028043968603014946,\n",
       "  -0.02088574692606926,\n",
       "  0.04665347561240196,\n",
       "  0.024873146787285805,\n",
       "  -0.005132364574819803,\n",
       "  -0.009467817842960358,\n",
       "  0.011916539631783962,\n",
       "  -0.006279473192989826,\n",
       "  0.05287991836667061,\n",
       "  -0.032927256077528,\n",
       "  -0.01802118867635727,\n",
       "  -0.015676407143473625,\n",
       "  -0.061968348920345306,\n",
       "  0.0010965541005134583,\n",
       "  0.04033398628234863,\n",
       "  -0.002759467577561736,\n",
       "  -0.01666276343166828,\n",
       "  -0.06741810590028763,\n",
       "  -0.010509883984923363,\n",
       "  -0.03336802124977112,\n",
       "  0.0173942893743515,\n",
       "  0.08185771107673645,\n",
       "  -0.026910360902547836,\n",
       "  -0.0024405913427472115,\n",
       "  -0.00812874548137188,\n",
       "  -0.017965389415621758,\n",
       "  0.010497896932065487,\n",
       "  0.05330222100019455,\n",
       "  -0.0007088427664712071,\n",
       "  -0.015655135735869408,\n",
       "  0.03459199145436287,\n",
       "  -0.036464083939790726,\n",
       "  0.005355046596378088,\n",
       "  0.03792322054505348,\n",
       "  -0.03603602200746536,\n",
       "  0.07277362793684006,\n",
       "  -0.06043435260653496,\n",
       "  0.027600063011050224,\n",
       "  -0.030592555180191994,\n",
       "  0.013536443002521992,\n",
       "  0.09059654921293259,\n",
       "  0.010674647986888885,\n",
       "  -0.002495951484888792,\n",
       "  -0.020405994728207588,\n",
       "  -0.021551918238401413,\n",
       "  0.0064261662773787975,\n",
       "  0.05170082673430443,\n",
       "  0.0068229795433580875,\n",
       "  0.027252353727817535,\n",
       "  -0.011984063312411308,\n",
       "  -0.04156777262687683,\n",
       "  -0.0006149184773676097,\n",
       "  0.03733106702566147,\n",
       "  -0.0288343895226717,\n",
       "  -0.19406117498874664,\n",
       "  0.0698607861995697,\n",
       "  -0.016287166625261307,\n",
       "  0.016635103151202202,\n",
       "  0.0714961439371109,\n",
       "  -0.021457495167851448,\n",
       "  0.019678818061947823,\n",
       "  0.025852475315332413,\n",
       "  -0.0003211961593478918,\n",
       "  0.024218324571847916,\n",
       "  0.03776644170284271,\n",
       "  -0.08856645971536636,\n",
       "  0.036357250064611435,\n",
       "  0.023393647745251656,\n",
       "  -0.06605221331119537,\n",
       "  0.01999928429722786,\n",
       "  0.05296934023499489,\n",
       "  0.03933567553758621,\n",
       "  0.05042385682463646,\n",
       "  0.008765820413827896,\n",
       "  -0.024945568293333054,\n",
       "  -0.029620159417390823,\n",
       "  -0.02226010337471962,\n",
       "  0.0023506528232246637,\n",
       "  0.02881813794374466,\n",
       "  0.026714906096458435,\n",
       "  0.03561989590525627,\n",
       "  -0.05285931006073952,\n",
       "  -0.013775055296719074,\n",
       "  -0.022112997248768806,\n",
       "  0.0016817408613860607,\n",
       "  -0.03267056494951248,\n",
       "  -0.02283569611608982,\n",
       "  0.008035112172365189,\n",
       "  -0.003579359734430909,\n",
       "  -0.03681528568267822,\n",
       "  0.02619735524058342,\n",
       "  0.012534914538264275,\n",
       "  0.01441444456577301,\n",
       "  0.03649143874645233,\n",
       "  -0.017456404864788055,\n",
       "  0.028792807832360268,\n",
       "  -0.051342692226171494,\n",
       "  -0.011202052235603333,\n",
       "  0.011605381965637207,\n",
       "  0.022049427032470703,\n",
       "  -0.009723189286887646,\n",
       "  -0.05084918439388275,\n",
       "  0.019710492342710495,\n",
       "  -0.010868750512599945,\n",
       "  0.008940251544117928,\n",
       "  0.02284051477909088,\n",
       "  -0.03991576284170151,\n",
       "  -0.07024314254522324,\n",
       "  0.01757083460688591,\n",
       "  0.06311193108558655,\n",
       "  0.013436184264719486,\n",
       "  0.03048068657517433,\n",
       "  -0.03392080217599869,\n",
       "  -0.04035210609436035,\n",
       "  0.06605595350265503,\n",
       "  0.02751486748456955,\n",
       "  -0.003759277518838644,\n",
       "  -0.032089173793792725,\n",
       "  0.014958945102989674,\n",
       "  0.00976578053086996,\n",
       "  0.024180326610803604,\n",
       "  0.005400355905294418,\n",
       "  0.0029008660931140184,\n",
       "  0.043678127229213715,\n",
       "  -0.016153359785676003,\n",
       "  0.03562330827116966,\n",
       "  -0.028840599581599236,\n",
       "  0.01890174299478531,\n",
       "  -0.017287423834204674,\n",
       "  -0.039110343903303146,\n",
       "  0.002298312494531274,\n",
       "  -0.006400281097739935,\n",
       "  0.02760256454348564,\n",
       "  0.03906264528632164,\n",
       "  0.004736338742077351,\n",
       "  -0.08097969740629196,\n",
       "  -0.019368575885891914,\n",
       "  0.04388347640633583,\n",
       "  -0.04299741983413696,\n",
       "  -0.0004005549708381295,\n",
       "  0.008104326203465462,\n",
       "  0.020982397720217705,\n",
       "  0.0367257259786129,\n",
       "  0.04310446232557297,\n",
       "  0.023800615221261978,\n",
       "  -0.03855784237384796,\n",
       "  0.032695185393095016,\n",
       "  0.011476102285087109,\n",
       "  0.02091461420059204,\n",
       "  0.009534887969493866,\n",
       "  0.04000695422291756,\n",
       "  0.05695810541510582,\n",
       "  -0.04444430023431778,\n",
       "  0.03595319762825966,\n",
       "  -0.03468840569257736,\n",
       "  -0.024425717070698738,\n",
       "  0.0022914051078259945,\n",
       "  0.004418071359395981,\n",
       "  0.06351293623447418,\n",
       "  -0.055295076221227646,\n",
       "  0.003411931451410055,\n",
       "  0.00729387765750289,\n",
       "  0.026690907776355743,\n",
       "  -0.019889628514647484,\n",
       "  0.027415204793214798,\n",
       "  0.00982938427478075,\n",
       "  0.015976540744304657,\n",
       "  0.014284291304647923,\n",
       "  0.046706877648830414,\n",
       "  -0.046480532735586166,\n",
       "  0.03223507106304169,\n",
       "  -0.04950449615716934,\n",
       "  -0.02791500650346279,\n",
       "  -0.034733645617961884,\n",
       "  -0.0085817975923419,\n",
       "  0.014688136987388134,\n",
       "  0.003208588343113661,\n",
       "  -0.04420141130685806,\n",
       "  0.0636928528547287,\n",
       "  -0.03197048604488373,\n",
       "  0.00896482728421688,\n",
       "  -0.0023297066800296307,\n",
       "  0.04734047129750252,\n",
       "  0.02129253000020981,\n",
       "  -0.04010821506381035,\n",
       "  0.03506479412317276,\n",
       "  0.03490198403596878,\n",
       "  0.007433653809130192,\n",
       "  -0.011877373792231083,\n",
       "  0.07417719811201096,\n",
       "  0.01252448558807373,\n",
       "  -0.006175404880195856,\n",
       "  0.0298219732940197,\n",
       "  0.02022179774940014,\n",
       "  0.0030211880803108215,\n",
       "  -0.015042068436741829,\n",
       "  0.0604298897087574,\n",
       "  -0.025835564360022545,\n",
       "  0.015385830774903297,\n",
       "  0.01964603178203106,\n",
       "  0.018673961982131004,\n",
       "  -0.02249871753156185,\n",
       "  -0.059501707553863525,\n",
       "  0.013976742513477802,\n",
       "  0.008225998841226101,\n",
       "  -0.0088047469034791,\n",
       "  -0.02357880212366581,\n",
       "  0.004663960076868534,\n",
       "  0.039100807160139084,\n",
       "  0.02401038073003292,\n",
       "  0.010940508916974068,\n",
       "  -0.0032406526152044535,\n",
       "  -0.08260941505432129,\n",
       "  0.0010839395690709352,\n",
       "  -0.0016761774895712733,\n",
       "  0.03453061729669571,\n",
       "  -0.010945230722427368,\n",
       "  0.040218256413936615,\n",
       "  -0.022501826286315918,\n",
       "  -0.06820309907197952,\n",
       "  0.06279224157333374,\n",
       "  0.008267971687018871,\n",
       "  -0.019311467185616493,\n",
       "  -0.03527776896953583,\n",
       "  0.008007751777768135,\n",
       "  -0.05877907946705818,\n",
       "  -0.0029967280570417643,\n",
       "  -0.03529711067676544,\n",
       "  -0.018483249470591545,\n",
       "  -0.00748842628672719,\n",
       "  0.053007688373327255,\n",
       "  -0.0801343023777008,\n",
       "  -0.02653629705309868,\n",
       "  0.020125020295381546],\n",
       " [0.027096280828118324,\n",
       "  0.018967973068356514,\n",
       "  -0.017958994954824448,\n",
       "  -0.04627048224210739,\n",
       "  0.026409411802887917,\n",
       "  -0.01090998388826847,\n",
       "  0.039756856858730316,\n",
       "  -0.029128875583410263,\n",
       "  -0.051787544041872025,\n",
       "  0.015923330560326576,\n",
       "  -0.034476038068532944,\n",
       "  0.01511534396559,\n",
       "  0.04957660287618637,\n",
       "  -0.05452098324894905,\n",
       "  -0.0015223801601678133,\n",
       "  0.000571153243072331,\n",
       "  0.005622328259050846,\n",
       "  -0.03524976968765259,\n",
       "  -0.00708194961771369,\n",
       "  -0.04195207357406616,\n",
       "  -0.018299851566553116,\n",
       "  0.002954478608444333,\n",
       "  -0.0005388821591623127,\n",
       "  0.04307102784514427,\n",
       "  0.009397360496222973,\n",
       "  0.0019993213936686516,\n",
       "  0.038061872124671936,\n",
       "  -0.04391885921359062,\n",
       "  -0.008187204599380493,\n",
       "  0.03902295231819153,\n",
       "  -0.008515723049640656,\n",
       "  -0.004613223485648632,\n",
       "  0.05329632759094238,\n",
       "  -0.03557104244828224,\n",
       "  -0.06373752653598785,\n",
       "  0.010578961111605167,\n",
       "  -0.044326506555080414,\n",
       "  -0.02408783510327339,\n",
       "  0.10208255797624588,\n",
       "  0.005126631818711758,\n",
       "  0.004870780743658543,\n",
       "  0.020332831889390945,\n",
       "  0.049261752516031265,\n",
       "  -0.06924613565206528,\n",
       "  -0.04435557499527931,\n",
       "  -0.029903467744588852,\n",
       "  0.00556972436606884,\n",
       "  -0.030664484947919846,\n",
       "  0.022296879440546036,\n",
       "  -0.01897340454161167,\n",
       "  -0.005275876726955175,\n",
       "  0.04533908888697624,\n",
       "  -0.003689095377922058,\n",
       "  0.001457309233956039,\n",
       "  0.030642395839095116,\n",
       "  -0.0410369411110878,\n",
       "  -0.02399950660765171,\n",
       "  -0.030078573152422905,\n",
       "  -0.0438050739467144,\n",
       "  0.010109304450452328,\n",
       "  0.014990043826401234,\n",
       "  0.016206173226237297,\n",
       "  0.06200524419546127,\n",
       "  -0.00579808047041297,\n",
       "  -0.003834189148619771,\n",
       "  0.0037847098428756,\n",
       "  -0.027648577466607094,\n",
       "  0.01350635290145874,\n",
       "  -0.007692336104810238,\n",
       "  -0.006893094163388014,\n",
       "  -0.0943516194820404,\n",
       "  0.022364137694239616,\n",
       "  -0.05000228434801102,\n",
       "  -0.014440455473959446,\n",
       "  0.0019163340330123901,\n",
       "  -0.0030478762928396463,\n",
       "  0.014708809554576874,\n",
       "  0.057199954986572266,\n",
       "  -0.011631046421825886,\n",
       "  0.03541490435600281,\n",
       "  0.028707459568977356,\n",
       "  -0.04675394296646118,\n",
       "  -0.033859651535749435,\n",
       "  0.017620643600821495,\n",
       "  -0.012478096410632133,\n",
       "  -0.05661123991012573,\n",
       "  -0.0018373893108218908,\n",
       "  0.02788776345551014,\n",
       "  0.04866473004221916,\n",
       "  0.0027126746717840433,\n",
       "  -0.001046748599037528,\n",
       "  0.03221515566110611,\n",
       "  0.11274969577789307,\n",
       "  0.02828351780772209,\n",
       "  0.04516765847802162,\n",
       "  0.019302774220705032,\n",
       "  0.015436237677931786,\n",
       "  -0.0348278246819973,\n",
       "  0.011481820605695248,\n",
       "  -0.025336654856801033,\n",
       "  -0.004619877319782972,\n",
       "  -0.097745381295681,\n",
       "  -0.03338414058089256,\n",
       "  0.039875440299510956,\n",
       "  0.06093933805823326,\n",
       "  -0.03568052127957344,\n",
       "  -0.011427603662014008,\n",
       "  -0.035409070551395416,\n",
       "  -0.026193013414740562,\n",
       "  -0.0019367842469364405,\n",
       "  0.03651672229170799,\n",
       "  0.05784500762820244,\n",
       "  0.038542453199625015,\n",
       "  -0.07655441015958786,\n",
       "  -0.00031380451400764287,\n",
       "  0.054088152945041656,\n",
       "  -0.010034219361841679,\n",
       "  -0.04385076463222504,\n",
       "  -0.022530606016516685,\n",
       "  -0.01335352472960949,\n",
       "  -0.046487145125865936,\n",
       "  -0.025029389187693596,\n",
       "  -0.01247957069426775,\n",
       "  0.03143971785902977,\n",
       "  -0.014676714316010475,\n",
       "  0.009093094617128372,\n",
       "  -0.027393437922000885,\n",
       "  -0.0409604087471962,\n",
       "  -0.02986929751932621,\n",
       "  -0.0456734225153923,\n",
       "  -0.03815766051411629,\n",
       "  0.07111749798059464,\n",
       "  -0.006503876764327288,\n",
       "  -0.005974300671368837,\n",
       "  -0.009874179027974606,\n",
       "  0.07949389517307281,\n",
       "  0.0035282575991004705,\n",
       "  0.0020767536479979753,\n",
       "  -0.005431049037724733,\n",
       "  -0.012958049774169922,\n",
       "  0.03239741548895836,\n",
       "  -0.13244637846946716,\n",
       "  0.04389902576804161,\n",
       "  0.04037777706980705,\n",
       "  -0.01737663894891739,\n",
       "  -0.06458762288093567,\n",
       "  -0.022466221824288368,\n",
       "  0.0057159652933478355,\n",
       "  0.09963878989219666,\n",
       "  0.04154626280069351,\n",
       "  0.03926132991909981,\n",
       "  -0.018081344664096832,\n",
       "  0.09453564882278442,\n",
       "  -0.001964457565918565,\n",
       "  -0.039633579552173615,\n",
       "  0.035669226199388504,\n",
       "  -0.019526194781064987,\n",
       "  -0.028034145012497902,\n",
       "  -0.035681743174791336,\n",
       "  0.014698298647999763,\n",
       "  0.06573714315891266,\n",
       "  -0.008324493654072285,\n",
       "  -0.043610040098428726,\n",
       "  -0.025797167792916298,\n",
       "  0.03789057210087776,\n",
       "  0.050562404096126556,\n",
       "  -0.0026111099869012833,\n",
       "  0.04779621958732605,\n",
       "  -0.05538465827703476,\n",
       "  -0.021074911579489708,\n",
       "  -0.04660893604159355,\n",
       "  0.010907964780926704,\n",
       "  -0.06486450880765915,\n",
       "  0.028366858139634132,\n",
       "  -0.017120959237217903,\n",
       "  0.02375577762722969,\n",
       "  -0.07107696682214737,\n",
       "  0.020467784255743027,\n",
       "  -0.002573878038674593,\n",
       "  -0.03932524472475052,\n",
       "  -0.07306740432977676,\n",
       "  0.003415526356548071,\n",
       "  0.03358687460422516,\n",
       "  0.01765282079577446,\n",
       "  0.0026743910275399685,\n",
       "  0.03457160294055939,\n",
       "  0.00850553996860981,\n",
       "  0.013310952112078667,\n",
       "  -0.010374413803219795,\n",
       "  0.029552876949310303,\n",
       "  0.05136365815997124,\n",
       "  0.04430127143859863,\n",
       "  0.013780700974166393,\n",
       "  -0.016594022512435913,\n",
       "  0.018800662830471992,\n",
       "  -0.00023898539075162262,\n",
       "  -0.0022083015646785498,\n",
       "  0.04949251934885979,\n",
       "  0.014623545110225677,\n",
       "  0.026958586648106575,\n",
       "  -0.03414548933506012,\n",
       "  0.02843945473432541,\n",
       "  -0.07766512781381607,\n",
       "  0.00621340237557888,\n",
       "  0.01762445829808712,\n",
       "  -0.020089847967028618,\n",
       "  0.03192969784140587,\n",
       "  -0.06502823531627655,\n",
       "  0.008271883241832256,\n",
       "  0.04863077029585838,\n",
       "  0.049785710871219635,\n",
       "  0.004796712193638086,\n",
       "  0.003914009314030409,\n",
       "  0.0020181939471513033,\n",
       "  0.019755367189645767,\n",
       "  0.07063642889261246,\n",
       "  0.04253154620528221,\n",
       "  -0.05608607456088066,\n",
       "  0.034589655697345734,\n",
       "  0.02442973665893078,\n",
       "  0.016186323016881943,\n",
       "  0.009204257279634476,\n",
       "  0.0026561629492789507,\n",
       "  0.002155564958229661,\n",
       "  -0.024052763357758522,\n",
       "  0.005137046333402395,\n",
       "  0.021430810913443565,\n",
       "  -0.0021429015323519707,\n",
       "  0.02068568952381611,\n",
       "  0.03088928945362568,\n",
       "  -0.015518034808337688,\n",
       "  -0.026333263143897057,\n",
       "  -0.008702276274561882,\n",
       "  0.0024325745180249214,\n",
       "  -0.017045170068740845,\n",
       "  0.061513785272836685,\n",
       "  0.02245262823998928,\n",
       "  -0.023311113938689232,\n",
       "  -0.007586135063320398,\n",
       "  -0.07446826249361038,\n",
       "  -0.023643625900149345,\n",
       "  -0.049108006060123444,\n",
       "  -0.01324805524200201,\n",
       "  0.02553577534854412,\n",
       "  0.05035664513707161,\n",
       "  0.021318823099136353,\n",
       "  -0.015997331589460373,\n",
       "  0.003564096288755536,\n",
       "  -0.052710819989442825,\n",
       "  -0.012402483262121677,\n",
       "  -0.01512510608881712,\n",
       "  0.002035753335803747,\n",
       "  -0.04472697153687477,\n",
       "  0.01707715541124344,\n",
       "  0.06777365505695343,\n",
       "  -0.026909029111266136,\n",
       "  0.06897791475057602,\n",
       "  0.04290791600942612,\n",
       "  -0.02979174256324768,\n",
       "  0.048500269651412964,\n",
       "  0.012118760496377945,\n",
       "  0.0037482527550309896,\n",
       "  -0.00347547116689384,\n",
       "  0.03384058177471161,\n",
       "  -0.011108671315014362,\n",
       "  0.0328686386346817,\n",
       "  0.026914438232779503,\n",
       "  0.03830474987626076,\n",
       "  0.050861459225416183,\n",
       "  0.019131794571876526,\n",
       "  0.02441490814089775,\n",
       "  -0.036313947290182114,\n",
       "  0.023368321359157562,\n",
       "  0.0037768075708299875,\n",
       "  -0.0043275244534015656,\n",
       "  -0.010029950179159641,\n",
       "  -0.021930428221821785,\n",
       "  0.033325549215078354,\n",
       "  0.02048681490123272,\n",
       "  0.008775861002504826,\n",
       "  -0.028858890756964684,\n",
       "  0.06723085045814514,\n",
       "  -0.0198945552110672,\n",
       "  -0.06019802391529083,\n",
       "  0.03539125621318817,\n",
       "  -0.07999850809574127,\n",
       "  -0.05842197313904762,\n",
       "  -0.00786561518907547,\n",
       "  0.08277592062950134,\n",
       "  -0.055438898503780365,\n",
       "  -0.031066201627254486,\n",
       "  0.0014256022404879332,\n",
       "  0.018813306465744972,\n",
       "  -0.025197137147188187,\n",
       "  0.060322441160678864,\n",
       "  -0.010240631178021431,\n",
       "  0.04315672814846039,\n",
       "  -0.012388443574309349,\n",
       "  0.07560225576162338,\n",
       "  0.0600617416203022,\n",
       "  0.02346307784318924,\n",
       "  0.00833346601575613,\n",
       "  0.03244845196604729,\n",
       "  -0.03659168630838394,\n",
       "  0.0218940582126379,\n",
       "  -0.0007377936271950603,\n",
       "  0.06916607916355133,\n",
       "  -0.0051268357783555984,\n",
       "  -0.00354320858605206,\n",
       "  0.028186233714222908,\n",
       "  -0.016140364110469818,\n",
       "  0.013287677429616451,\n",
       "  -0.02668839879333973,\n",
       "  -0.017414607107639313,\n",
       "  0.004510442726314068,\n",
       "  0.004726756364107132,\n",
       "  -0.009147843345999718,\n",
       "  0.0037406538613140583,\n",
       "  0.017743606120347977,\n",
       "  -0.014850237406790257,\n",
       "  -0.032620783895254135,\n",
       "  0.013820433057844639,\n",
       "  -0.006117356475442648,\n",
       "  -0.01757439784705639,\n",
       "  -0.026564834639430046,\n",
       "  0.0074613760225474834,\n",
       "  0.04598931223154068,\n",
       "  0.014799285680055618,\n",
       "  -0.014527789317071438,\n",
       "  -0.009420034475624561,\n",
       "  -0.03295574709773064,\n",
       "  -0.03559233248233795,\n",
       "  0.0028446309734135866,\n",
       "  -0.0012088081566616893,\n",
       "  -0.027090178802609444,\n",
       "  -0.0003445996262598783,\n",
       "  0.02661873586475849,\n",
       "  -0.04692598432302475,\n",
       "  0.04489409178495407,\n",
       "  -0.04471034184098244,\n",
       "  0.013568294234573841,\n",
       "  0.038705550134181976,\n",
       "  0.03185003623366356,\n",
       "  -0.05930484086275101,\n",
       "  -0.009967764839529991,\n",
       "  -0.01297268271446228,\n",
       "  -0.013585472479462624,\n",
       "  0.006242698058485985,\n",
       "  -0.02284107729792595,\n",
       "  -0.018036095425486565,\n",
       "  0.027075156569480896,\n",
       "  -0.05840626731514931,\n",
       "  0.015560717321932316,\n",
       "  -0.04340875521302223,\n",
       "  0.034532420337200165,\n",
       "  -0.026634790003299713,\n",
       "  0.08067366480827332,\n",
       "  -0.07020838558673859,\n",
       "  -0.009547636844217777,\n",
       "  0.02159366011619568,\n",
       "  -0.030818145722150803,\n",
       "  0.01887350343167782,\n",
       "  -0.05096729099750519,\n",
       "  0.04248320311307907,\n",
       "  0.001634326297789812,\n",
       "  0.0419844351708889,\n",
       "  0.07057158648967743,\n",
       "  0.0806678831577301,\n",
       "  0.05069148540496826,\n",
       "  -0.009082941338419914,\n",
       "  0.09744017571210861,\n",
       "  0.015943342819809914,\n",
       "  0.0516793392598629,\n",
       "  0.009401291608810425,\n",
       "  0.010789325460791588,\n",
       "  -0.018696483224630356,\n",
       "  0.015403423458337784,\n",
       "  -0.03855916112661362,\n",
       "  0.01892242580652237,\n",
       "  0.0630807876586914,\n",
       "  -0.0291625764220953,\n",
       "  -0.008915743790566921,\n",
       "  0.006567729637026787,\n",
       "  -0.022965187206864357,\n",
       "  0.004697924945503473,\n",
       "  0.014197640120983124,\n",
       "  0.004382005892693996,\n",
       "  -0.018264809623360634,\n",
       "  0.0169537290930748,\n",
       "  0.07070830464363098,\n",
       "  -0.0009250978473573923,\n",
       "  -0.06593320518732071,\n",
       "  -0.028300700709223747,\n",
       "  -0.04239964485168457,\n",
       "  0.01736302860081196,\n",
       "  -0.061397336423397064,\n",
       "  -0.0019589774310588837,\n",
       "  0.020249871537089348,\n",
       "  0.01070442609488964,\n",
       "  -0.08253875374794006,\n",
       "  -0.00548699963837862,\n",
       "  0.010903988964855671,\n",
       "  -0.01614801026880741,\n",
       "  0.056031499058008194,\n",
       "  -0.012278346344828606,\n",
       "  0.014833117835223675,\n",
       "  0.007428862154483795,\n",
       "  0.010233934037387371,\n",
       "  -0.0036074661184102297,\n",
       "  -0.008536885492503643,\n",
       "  0.016188718378543854,\n",
       "  0.016621727496385574,\n",
       "  0.009969661012291908,\n",
       "  0.01407594047486782,\n",
       "  -0.0011543730506673455,\n",
       "  -0.01409524492919445,\n",
       "  -0.03316241502761841,\n",
       "  0.04271276295185089,\n",
       "  -0.006768310442566872,\n",
       "  -0.013605437241494656,\n",
       "  -0.003471838543191552,\n",
       "  -0.030368492007255554,\n",
       "  0.023629313334822655,\n",
       "  -0.010561609640717506,\n",
       "  -0.005444539710879326,\n",
       "  -0.0029813956934958696,\n",
       "  0.05075841024518013,\n",
       "  -0.06280110031366348,\n",
       "  -0.013021226041018963,\n",
       "  -0.01413100678473711,\n",
       "  0.009837368503212929,\n",
       "  0.021282406523823738,\n",
       "  0.02199879102408886,\n",
       "  -0.05843421071767807,\n",
       "  -0.014260721392929554,\n",
       "  -0.03522079437971115,\n",
       "  0.026962723582983017,\n",
       "  0.002480511087924242,\n",
       "  0.02730526588857174,\n",
       "  0.051369134336709976,\n",
       "  -0.018026769161224365,\n",
       "  0.021972211077809334,\n",
       "  0.004406084772199392,\n",
       "  0.05158553645014763,\n",
       "  0.005619818810373545,\n",
       "  0.028604766353964806,\n",
       "  -0.02664092183113098,\n",
       "  -0.023921718820929527,\n",
       "  -0.020661644637584686,\n",
       "  -0.003573131514713168,\n",
       "  0.006978831719607115,\n",
       "  0.010281946510076523,\n",
       "  0.007450180593878031,\n",
       "  -0.021349208429455757,\n",
       "  -0.040378108620643616,\n",
       "  -0.03540317341685295,\n",
       "  0.019864266738295555,\n",
       "  0.01744234189391136,\n",
       "  -0.003454763675108552,\n",
       "  -0.0943329930305481,\n",
       "  0.04373529925942421,\n",
       "  -0.061633672565221786,\n",
       "  -0.06335514783859253,\n",
       "  -0.10086481273174286,\n",
       "  0.03868468105792999,\n",
       "  -0.05684392526745796,\n",
       "  0.03990961238741875,\n",
       "  -0.04939620569348335,\n",
       "  -0.04846356809139252,\n",
       "  -0.014637740328907967,\n",
       "  0.003661087714135647,\n",
       "  0.024784518405795097,\n",
       "  0.007890709675848484,\n",
       "  -0.08038920909166336,\n",
       "  -0.015892405062913895,\n",
       "  0.016302786767482758,\n",
       "  0.008916058577597141,\n",
       "  0.013247372582554817,\n",
       "  -0.016636181622743607,\n",
       "  -0.01281858142465353,\n",
       "  0.016880732029676437,\n",
       "  -0.010209523141384125,\n",
       "  -0.006345055997371674,\n",
       "  -0.005956106353551149,\n",
       "  0.010735498741269112,\n",
       "  0.012948078103363514,\n",
       "  -0.022860871627926826,\n",
       "  -0.015325597487390041,\n",
       "  0.021749865263700485,\n",
       "  -0.020030612125992775,\n",
       "  0.006343485321849585,\n",
       "  0.05085107311606407,\n",
       "  -0.05128202959895134,\n",
       "  -0.010187377221882343,\n",
       "  -0.030849745497107506,\n",
       "  0.003129412652924657,\n",
       "  -0.0089286332949996,\n",
       "  0.016572656109929085,\n",
       "  0.08467931300401688,\n",
       "  0.019484149292111397,\n",
       "  -0.03664421662688255,\n",
       "  0.020628344267606735,\n",
       "  0.008756056427955627,\n",
       "  0.02743975631892681,\n",
       "  -0.04760785028338432,\n",
       "  0.025133786723017693,\n",
       "  -0.007782500237226486,\n",
       "  -0.01820087619125843,\n",
       "  -0.007606238126754761,\n",
       "  0.02165711671113968,\n",
       "  0.027017612010240555,\n",
       "  0.038130175322294235,\n",
       "  0.06114107370376587,\n",
       "  -0.01941283419728279,\n",
       "  0.021987954154610634,\n",
       "  0.01926819421350956,\n",
       "  0.0022508418187499046,\n",
       "  -0.00047062759404070675,\n",
       "  -0.0025536397006362677,\n",
       "  -0.025323525071144104,\n",
       "  0.008578917942941189,\n",
       "  -0.04702186584472656,\n",
       "  -0.009474595077335835,\n",
       "  0.03739644214510918,\n",
       "  0.028638014569878578,\n",
       "  0.010773600079119205,\n",
       "  -0.01073937676846981,\n",
       "  0.014833806082606316,\n",
       "  -0.03380623832345009,\n",
       "  -0.0370318666100502,\n",
       "  -0.06871090829372406,\n",
       "  0.07826941460371017,\n",
       "  -0.017295589670538902,\n",
       "  -0.03752901032567024,\n",
       "  0.02697223238646984,\n",
       "  0.0566130056977272,\n",
       "  0.03927561268210411,\n",
       "  0.016347544267773628,\n",
       "  0.048092834651470184,\n",
       "  -0.015091747976839542,\n",
       "  0.04463429003953934,\n",
       "  0.0032897184137254953,\n",
       "  -0.02125677652657032,\n",
       "  -0.034951917827129364,\n",
       "  -0.048800792545080185,\n",
       "  -0.031018581241369247,\n",
       "  -0.01647324115037918,\n",
       "  0.015497429296374321,\n",
       "  0.037629399448633194,\n",
       "  -0.008833413012325764,\n",
       "  -0.051335908472537994,\n",
       "  0.05552360415458679,\n",
       "  -0.03669840097427368,\n",
       "  0.02701110951602459,\n",
       "  0.02342405915260315,\n",
       "  0.05421361327171326,\n",
       "  -0.055467698723077774,\n",
       "  0.06376703828573227,\n",
       "  -0.03232626989483833,\n",
       "  -0.033846668899059296,\n",
       "  0.04000047966837883,\n",
       "  0.012156769633293152,\n",
       "  -0.02339109405875206,\n",
       "  -0.026995891705155373,\n",
       "  0.015162366442382336,\n",
       "  -0.0042580487206578255,\n",
       "  0.014645818620920181,\n",
       "  -0.011891731061041355,\n",
       "  -0.01630495674908161,\n",
       "  0.11611471325159073,\n",
       "  -0.05255928635597229,\n",
       "  -0.019279535859823227,\n",
       "  -0.025156311690807343,\n",
       "  -0.005579694639891386,\n",
       "  0.04889482259750366,\n",
       "  0.021037962287664413,\n",
       "  0.0021363217383623123,\n",
       "  -0.014303869567811489,\n",
       "  0.030109481886029243,\n",
       "  -0.05212922394275665,\n",
       "  0.03903941437602043,\n",
       "  -0.0604548342525959,\n",
       "  0.01298454962670803,\n",
       "  0.035393740981817245,\n",
       "  -0.010414534248411655,\n",
       "  -0.020104005932807922,\n",
       "  -0.026388468220829964,\n",
       "  -0.029188431799411774,\n",
       "  -0.19121427834033966,\n",
       "  0.08840090781450272,\n",
       "  -0.05989930406212807,\n",
       "  -0.016059566289186478,\n",
       "  0.013317050412297249,\n",
       "  -0.004612291697412729,\n",
       "  0.04694851115345955,\n",
       "  -0.00341798085719347,\n",
       "  -0.024361152201890945,\n",
       "  -0.014376642182469368,\n",
       "  0.03377392515540123,\n",
       "  -0.02582750841975212,\n",
       "  0.057522252202034,\n",
       "  0.012045348063111305,\n",
       "  0.007688191253691912,\n",
       "  0.025273321196436882,\n",
       "  0.006460848264396191,\n",
       "  0.011336922645568848,\n",
       "  0.03346508741378784,\n",
       "  -0.009023649618029594,\n",
       "  -0.016020072624087334,\n",
       "  0.04934563487768173,\n",
       "  0.005389303434640169,\n",
       "  -0.02133745141327381,\n",
       "  -0.002793330932036042,\n",
       "  -0.03676547110080719,\n",
       "  0.03636694326996803,\n",
       "  -0.052736133337020874,\n",
       "  -0.01183309406042099,\n",
       "  -0.012634705752134323,\n",
       "  0.00018688463023863733,\n",
       "  -0.027062540873885155,\n",
       "  -0.028325216844677925,\n",
       "  0.0006581445340998471,\n",
       "  -0.0027543092146515846,\n",
       "  0.0022561459336429834,\n",
       "  0.035963211208581924,\n",
       "  -0.03453235700726509,\n",
       "  0.06592978537082672,\n",
       "  0.06395257264375687,\n",
       "  -0.010120737366378307,\n",
       "  -0.0692262202501297,\n",
       "  0.012942882254719734,\n",
       "  0.0022038512397557497,\n",
       "  0.043239716440439224,\n",
       "  0.03267316520214081,\n",
       "  -0.0020895940251648426,\n",
       "  -0.012406079098582268,\n",
       "  0.007601923309266567,\n",
       "  0.020519793033599854,\n",
       "  0.08168815821409225,\n",
       "  -0.018503237515687943,\n",
       "  0.025008022785186768,\n",
       "  -0.015907898545265198,\n",
       "  -0.03317776322364807,\n",
       "  0.04611288383603096,\n",
       "  0.03746500611305237,\n",
       "  0.023090040311217308,\n",
       "  -0.03356489911675453,\n",
       "  -0.047270093113183975,\n",
       "  0.053438086062669754,\n",
       "  0.009568396955728531,\n",
       "  0.005333452019840479,\n",
       "  0.04172353819012642,\n",
       "  0.0018020246643573046,\n",
       "  0.0330180749297142,\n",
       "  0.05742717906832695,\n",
       "  0.031891658902168274,\n",
       "  -0.07881810516119003,\n",
       "  0.023113638162612915,\n",
       "  0.03372003138065338,\n",
       "  -0.0030735048931092024,\n",
       "  -0.02289295196533203,\n",
       "  0.047773949801921844,\n",
       "  -0.023439113050699234,\n",
       "  -0.029116190969944,\n",
       "  -0.0347725972533226,\n",
       "  -0.012282397598028183,\n",
       "  0.009709417819976807,\n",
       "  -0.003306443803012371,\n",
       "  0.01347640622407198,\n",
       "  -0.02551283873617649,\n",
       "  -0.037229832261800766,\n",
       "  -0.02561820298433304,\n",
       "  -0.0036907829344272614,\n",
       "  0.0003131505218334496,\n",
       "  0.06199793145060539,\n",
       "  -0.00013575410412158817,\n",
       "  -0.0028976411558687687,\n",
       "  -0.008292663842439651,\n",
       "  0.055079102516174316,\n",
       "  -0.015620714984834194,\n",
       "  -0.06738347560167313,\n",
       "  -0.03388325497508049,\n",
       "  -0.01284704264253378,\n",
       "  0.05376196280121803,\n",
       "  0.04763582721352577,\n",
       "  -0.0014993983786553144,\n",
       "  -0.040413424372673035,\n",
       "  0.014348294585943222,\n",
       "  -0.023685641586780548,\n",
       "  -0.019984569400548935,\n",
       "  -0.03633240982890129,\n",
       "  -0.022199582308530807,\n",
       "  -0.0021233255974948406,\n",
       "  0.058509111404418945,\n",
       "  -0.00015163687930908054,\n",
       "  0.041763387620449066,\n",
       "  -0.02461550571024418,\n",
       "  -0.04739364981651306,\n",
       "  0.018712138757109642,\n",
       "  0.03183191642165184,\n",
       "  0.005479193292558193,\n",
       "  0.0046105957590043545,\n",
       "  0.04285851866006851,\n",
       "  -0.042950283735990524,\n",
       "  -0.0027519960422068834,\n",
       "  -0.020873263478279114,\n",
       "  0.01886388286948204,\n",
       "  -0.02555399388074875,\n",
       "  -0.01123805996030569,\n",
       "  0.017947588115930557,\n",
       "  -0.006786801386624575,\n",
       "  -0.04414236173033714,\n",
       "  0.04063713178038597,\n",
       "  0.010402915067970753,\n",
       "  0.04524214193224907,\n",
       "  -0.03632751852273941,\n",
       "  0.01740371435880661,\n",
       "  -0.026432495564222336,\n",
       "  0.03665321320295334,\n",
       "  0.025960028171539307,\n",
       "  0.04920012131333351,\n",
       "  -0.011769811622798443,\n",
       "  0.026667552068829536,\n",
       "  0.009621826931834221,\n",
       "  0.0035325686912983656,\n",
       "  -0.003954706247895956,\n",
       "  -0.0008732362184673548,\n",
       "  -0.004155727569013834,\n",
       "  -0.023176658898591995,\n",
       "  -0.07821020483970642,\n",
       "  0.019849665462970734,\n",
       "  -0.03663152828812599,\n",
       "  0.037045519798994064,\n",
       "  0.016588715836405754,\n",
       "  0.06441285461187363,\n",
       "  -0.040800511837005615,\n",
       "  -0.058654993772506714,\n",
       "  -0.01876666396856308,\n",
       "  0.050279490649700165,\n",
       "  0.002432924695312977,\n",
       "  0.025643358007073402,\n",
       "  0.06832429021596909,\n",
       "  0.046082448214292526,\n",
       "  -0.016544649377465248,\n",
       "  0.025079362094402313,\n",
       "  -0.03375642001628876,\n",
       "  0.0026663646567612886,\n",
       "  -0.0061819045804440975,\n",
       "  -0.07307300716638565,\n",
       "  -0.0345199853181839,\n",
       "  -0.03818501904606819,\n",
       "  0.013165614567697048,\n",
       "  0.00040920532774180174,\n",
       "  -0.03128204494714737,\n",
       "  0.010690555907785892,\n",
       "  0.08408173173666,\n",
       "  0.0348467156291008,\n",
       "  -0.018221493810415268,\n",
       "  -0.022863313555717468,\n",
       "  -0.027603764086961746,\n",
       "  -0.04048578441143036,\n",
       "  -0.039723750203847885,\n",
       "  -0.04497547820210457,\n",
       "  -0.053121648728847504,\n",
       "  0.0721004381775856,\n",
       "  -0.016567952930927277,\n",
       "  0.005975900683552027,\n",
       "  -0.036758262664079666],\n",
       " [0.030090220272541046,\n",
       "  0.03022252954542637,\n",
       "  1.1774696758948267e-05,\n",
       "  -0.0206633098423481,\n",
       "  0.007408387027680874,\n",
       "  0.02867852710187435,\n",
       "  -0.013822613283991814,\n",
       "  0.0030066773761063814,\n",
       "  -0.018836792558431625,\n",
       "  -0.004571125376969576,\n",
       "  -0.009724934585392475,\n",
       "  0.02156027965247631,\n",
       "  0.08728186786174774,\n",
       "  0.005276060197502375,\n",
       "  0.018368592485785484,\n",
       "  0.02247835509479046,\n",
       "  -0.028418023139238358,\n",
       "  -0.015118570066988468,\n",
       "  0.004175774287432432,\n",
       "  0.06509600579738617,\n",
       "  0.0511980801820755,\n",
       "  -0.0045174891129136086,\n",
       "  0.051005490124225616,\n",
       "  0.09316127747297287,\n",
       "  0.09387630224227905,\n",
       "  -0.013621797785162926,\n",
       "  0.060193777084350586,\n",
       "  -0.00902024656534195,\n",
       "  -0.039890486747026443,\n",
       "  -0.013201330788433552,\n",
       "  -0.009269693866372108,\n",
       "  -0.021005213260650635,\n",
       "  0.006291287951171398,\n",
       "  -0.024356652051210403,\n",
       "  -0.033431779593229294,\n",
       "  0.0012383965076878667,\n",
       "  -0.03739403560757637,\n",
       "  -0.040164463222026825,\n",
       "  0.05391396954655647,\n",
       "  0.0020432951860129833,\n",
       "  0.013297172263264656,\n",
       "  0.0059738801792263985,\n",
       "  -0.006876588333398104,\n",
       "  -0.013103739358484745,\n",
       "  0.01988319121301174,\n",
       "  0.012429849244654179,\n",
       "  0.02221900410950184,\n",
       "  -0.011848879978060722,\n",
       "  0.03229673206806183,\n",
       "  -0.026891447603702545,\n",
       "  -0.06163247302174568,\n",
       "  -0.01675156131386757,\n",
       "  -0.012957138009369373,\n",
       "  0.008024372160434723,\n",
       "  0.07176320999860764,\n",
       "  -0.025528214871883392,\n",
       "  -0.019624603912234306,\n",
       "  0.01897495426237583,\n",
       "  0.022541850805282593,\n",
       "  -0.004408078268170357,\n",
       "  0.02227722480893135,\n",
       "  0.0021651596762239933,\n",
       "  0.008968706242740154,\n",
       "  -0.03768626227974892,\n",
       "  -0.056615665555000305,\n",
       "  -0.02950194478034973,\n",
       "  -0.002663659630343318,\n",
       "  0.0024043768644332886,\n",
       "  -0.0012984088389202952,\n",
       "  0.004174293950200081,\n",
       "  -0.05270131304860115,\n",
       "  -0.009056788869202137,\n",
       "  -0.014667997136712074,\n",
       "  0.037874072790145874,\n",
       "  -0.020768914371728897,\n",
       "  -0.02282712794840336,\n",
       "  -0.012020204216241837,\n",
       "  0.020729389041662216,\n",
       "  0.01049711462110281,\n",
       "  0.03591510280966759,\n",
       "  0.005189763847738504,\n",
       "  -0.020427731797099113,\n",
       "  0.0006385438027791679,\n",
       "  -0.028038786724209785,\n",
       "  -0.051326971501111984,\n",
       "  0.007061502896249294,\n",
       "  -0.015187508426606655,\n",
       "  -0.013333979062736034,\n",
       "  -0.0030712794978171587,\n",
       "  0.045229922980070114,\n",
       "  -0.0072335004806518555,\n",
       "  0.011021440848708153,\n",
       "  0.07657179236412048,\n",
       "  0.024609116837382317,\n",
       "  0.007458188105374575,\n",
       "  0.0010536732152104378,\n",
       "  0.01646842621266842,\n",
       "  -5.3520074288826436e-05,\n",
       "  0.005421792156994343,\n",
       "  -0.009619207121431828,\n",
       "  -0.029764287173748016,\n",
       "  -0.24237553775310516,\n",
       "  -0.016093511134386063,\n",
       "  0.0245226863771677,\n",
       "  -0.07390538603067398,\n",
       "  -0.009322215802967548,\n",
       "  -0.005037958268076181,\n",
       "  0.00415523536503315,\n",
       "  -0.01873175986111164,\n",
       "  0.014080971479415894,\n",
       "  0.0022781467996537685,\n",
       "  0.05196680128574371,\n",
       "  -0.005297658499330282,\n",
       "  -0.06680023670196533,\n",
       "  -0.07185985893011093,\n",
       "  -0.01801171898841858,\n",
       "  -0.0327402725815773,\n",
       "  -0.02866869606077671,\n",
       "  -0.00012340507237240672,\n",
       "  -0.00974281970411539,\n",
       "  0.01727764867246151,\n",
       "  0.009440390393137932,\n",
       "  -0.03503973037004471,\n",
       "  0.0033284274395555258,\n",
       "  0.026981297880411148,\n",
       "  0.0005656767752952874,\n",
       "  0.0005408846773207188,\n",
       "  -0.021746648475527763,\n",
       "  0.0017493567429482937,\n",
       "  -0.027775418013334274,\n",
       "  -0.022552667185664177,\n",
       "  0.01034901849925518,\n",
       "  0.030583294108510017,\n",
       "  0.018049102276563644,\n",
       "  -0.013990531675517559,\n",
       "  -0.0346379354596138,\n",
       "  0.0258554108440876,\n",
       "  -0.014962502755224705,\n",
       "  0.02690415270626545,\n",
       "  0.06503225117921829,\n",
       "  0.019143152981996536,\n",
       "  -0.05576173961162567,\n",
       "  -0.014768497087061405,\n",
       "  -0.032831497490406036,\n",
       "  -0.0307203009724617,\n",
       "  0.02547481469810009,\n",
       "  0.017027759924530983,\n",
       "  -0.004452168010175228,\n",
       "  0.0853014662861824,\n",
       "  0.006359032820910215,\n",
       "  0.06532690674066544,\n",
       "  -0.07302463799715042,\n",
       "  0.07539266347885132,\n",
       "  -0.011068045161664486,\n",
       "  -0.020007340237498283,\n",
       "  -0.014959662221372128,\n",
       "  -0.08660542219877243,\n",
       "  0.005782156717032194,\n",
       "  0.04272851347923279,\n",
       "  -0.02974972128868103,\n",
       "  0.05286557972431183,\n",
       "  0.0020225271582603455,\n",
       "  -0.013377277180552483,\n",
       "  0.020629510283470154,\n",
       "  0.058814335614442825,\n",
       "  0.043476276099681854,\n",
       "  -0.008145139552652836,\n",
       "  -0.038882941007614136,\n",
       "  -0.053016409277915955,\n",
       "  -0.004655058961361647,\n",
       "  -0.0039421552792191505,\n",
       "  0.007362843956798315,\n",
       "  -0.007034425623714924,\n",
       "  -0.0023794558364897966,\n",
       "  -0.029384689405560493,\n",
       "  0.04002898186445236,\n",
       "  0.002204128308221698,\n",
       "  0.008490710519254208,\n",
       "  0.03861613944172859,\n",
       "  -0.027670836076140404,\n",
       "  -0.07353448122739792,\n",
       "  0.010465793311595917,\n",
       "  0.042193785309791565,\n",
       "  0.001055917120538652,\n",
       "  0.018769385293126106,\n",
       "  -0.0031362026929855347,\n",
       "  -0.01144991721957922,\n",
       "  0.027143293991684914,\n",
       "  -0.01887764409184456,\n",
       "  0.004786582197993994,\n",
       "  0.004789367783814669,\n",
       "  -0.010677210986614227,\n",
       "  0.05667327716946602,\n",
       "  0.01761232130229473,\n",
       "  0.03127207234501839,\n",
       "  -0.007445888593792915,\n",
       "  -0.05500202253460884,\n",
       "  -0.011546680703759193,\n",
       "  -0.006989036686718464,\n",
       "  -0.006380112841725349,\n",
       "  0.0032285908237099648,\n",
       "  0.0008979108533822,\n",
       "  -0.0976756140589714,\n",
       "  0.03256635367870331,\n",
       "  -0.002035872545093298,\n",
       "  -0.014012642204761505,\n",
       "  0.03434111550450325,\n",
       "  0.027560463175177574,\n",
       "  0.005272324196994305,\n",
       "  -0.02057296596467495,\n",
       "  0.010774842463433743,\n",
       "  0.0005314011941663921,\n",
       "  -0.012357269413769245,\n",
       "  0.024906646460294724,\n",
       "  -0.014141419902443886,\n",
       "  0.056730326265096664,\n",
       "  0.037629205733537674,\n",
       "  0.01890680566430092,\n",
       "  0.020419824868440628,\n",
       "  0.06103961914777756,\n",
       "  0.03597304970026016,\n",
       "  -0.034152887761592865,\n",
       "  0.004972490947693586,\n",
       "  0.021449917927384377,\n",
       "  0.02255912870168686,\n",
       "  0.00032741131144575775,\n",
       "  0.017765412107110023,\n",
       "  0.005695076659321785,\n",
       "  -0.011525657959282398,\n",
       "  -0.03862316906452179,\n",
       "  -0.0017082120757550001,\n",
       "  -0.05236566439270973,\n",
       "  0.04142976552248001,\n",
       "  0.0036015918012708426,\n",
       "  0.015649739652872086,\n",
       "  -0.00980480294674635,\n",
       "  0.018011420965194702,\n",
       "  -0.014996285550296307,\n",
       "  -0.07458405196666718,\n",
       "  -0.0005650680977851152,\n",
       "  -0.012627343647181988,\n",
       "  -0.049299709498882294,\n",
       "  -0.02371974103152752,\n",
       "  -0.017734717577695847,\n",
       "  0.02179616689682007,\n",
       "  -0.013116511516273022,\n",
       "  0.0057226200588047504,\n",
       "  0.005139090586453676,\n",
       "  -0.03143281489610672,\n",
       "  -0.05308710038661957,\n",
       "  -0.0367276594042778,\n",
       "  -0.006892055738717318,\n",
       "  -0.002105701481923461,\n",
       "  -0.0025041888002306223,\n",
       "  0.059977103024721146,\n",
       "  0.0228425245732069,\n",
       "  -0.04240608587861061,\n",
       "  0.08394207805395126,\n",
       "  -0.011373263783752918,\n",
       "  0.06924797594547272,\n",
       "  -0.025257695466279984,\n",
       "  -0.009546561166644096,\n",
       "  0.03933899477124214,\n",
       "  -0.08141378313302994,\n",
       "  -0.004993482958525419,\n",
       "  0.003803000319749117,\n",
       "  -0.02163057029247284,\n",
       "  0.03266138583421707,\n",
       "  0.06548479199409485,\n",
       "  0.0018154499121010303,\n",
       "  0.019306695088744164,\n",
       "  -0.04553850740194321,\n",
       "  -0.00569534907117486,\n",
       "  0.007986276410520077,\n",
       "  0.019007500261068344,\n",
       "  0.04447165131568909,\n",
       "  -0.06744930893182755,\n",
       "  -0.04992146044969559,\n",
       "  0.001232809852808714,\n",
       "  -0.0017129527404904366,\n",
       "  -0.009011378511786461,\n",
       "  -0.012912384234368801,\n",
       "  -0.01259413082152605,\n",
       "  -0.005033302586525679,\n",
       "  0.03972805663943291,\n",
       "  -0.03861632198095322,\n",
       "  -0.06897290050983429,\n",
       "  -0.026685908436775208,\n",
       "  0.0020823972299695015,\n",
       "  -0.0056806160137057304,\n",
       "  -0.04136451706290245,\n",
       "  0.08918280899524689,\n",
       "  0.01863666996359825,\n",
       "  -0.015110570006072521,\n",
       "  0.09142816811800003,\n",
       "  -0.01369275338947773,\n",
       "  -0.007932140491902828,\n",
       "  -0.004241164308041334,\n",
       "  0.04746348038315773,\n",
       "  0.03844397887587547,\n",
       "  -0.0009883283637464046,\n",
       "  -0.002782220486551523,\n",
       "  -0.014492961578071117,\n",
       "  -0.01841108873486519,\n",
       "  0.0071923681534826756,\n",
       "  0.015136154368519783,\n",
       "  0.06550263613462448,\n",
       "  0.0003518060257192701,\n",
       "  -0.014070897363126278,\n",
       "  0.008444717153906822,\n",
       "  0.02451116032898426,\n",
       "  0.02287334017455578,\n",
       "  0.0044081793166697025,\n",
       "  0.020085984840989113,\n",
       "  -0.021829305216670036,\n",
       "  0.06352337449789047,\n",
       "  0.07127490639686584,\n",
       "  0.05287369340658188,\n",
       "  -0.021545223891735077,\n",
       "  0.021440085023641586,\n",
       "  0.02014220505952835,\n",
       "  7.199514948297292e-05,\n",
       "  0.011639287695288658,\n",
       "  -0.017903750762343407,\n",
       "  -0.005846073385328054,\n",
       "  -0.017661115154623985,\n",
       "  -0.03352118656039238,\n",
       "  0.015750430524349213,\n",
       "  0.021870972588658333,\n",
       "  -0.019629530608654022,\n",
       "  -0.013348929584026337,\n",
       "  -0.021665137261152267,\n",
       "  0.015511241741478443,\n",
       "  -0.035656414926052094,\n",
       "  0.0007717293919995427,\n",
       "  0.0002843054535333067,\n",
       "  -0.026884187012910843,\n",
       "  0.003735923208296299,\n",
       "  0.05996385216712952,\n",
       "  0.009393897838890553,\n",
       "  0.02235027402639389,\n",
       "  0.02328651398420334,\n",
       "  0.07360993325710297,\n",
       "  -0.0033924102317541838,\n",
       "  -0.04347512125968933,\n",
       "  -0.055830951780080795,\n",
       "  0.012552742846310139,\n",
       "  -0.07645280659198761,\n",
       "  0.005758084822446108,\n",
       "  0.006363812834024429,\n",
       "  0.02441740408539772,\n",
       "  0.04273444786667824,\n",
       "  0.0851016715168953,\n",
       "  -0.027527600526809692,\n",
       "  0.018648609519004822,\n",
       "  0.004049278795719147,\n",
       "  0.048824161291122437,\n",
       "  -0.05287778005003929,\n",
       "  -0.03462783247232437,\n",
       "  0.015317379496991634,\n",
       "  -0.005920666269958019,\n",
       "  0.033491313457489014,\n",
       "  -0.03259599581360817,\n",
       "  0.050725121051073074,\n",
       "  -0.011021786369383335,\n",
       "  0.03611333668231964,\n",
       "  0.019175315275788307,\n",
       "  0.05223202705383301,\n",
       "  0.007801368832588196,\n",
       "  -0.049631815403699875,\n",
       "  0.0697818174958229,\n",
       "  0.021994689479470253,\n",
       "  0.004481264855712652,\n",
       "  0.006805999204516411,\n",
       "  0.020497139543294907,\n",
       "  -0.01411073561757803,\n",
       "  -0.0032985976431518793,\n",
       "  -0.09529146552085876,\n",
       "  0.001349428202956915,\n",
       "  -0.01044989563524723,\n",
       "  -0.016504188999533653,\n",
       "  0.016901960596442223,\n",
       "  0.026731736958026886,\n",
       "  0.04443323612213135,\n",
       "  0.010851179249584675,\n",
       "  -0.015598486177623272,\n",
       "  0.038104601204395294,\n",
       "  0.050288327038288116,\n",
       "  0.016673173755407333,\n",
       "  0.03630823642015457,\n",
       "  -0.0014414022443816066,\n",
       "  -0.024946488440036774,\n",
       "  0.012356456369161606,\n",
       "  -0.06882592290639877,\n",
       "  -0.008798784576356411,\n",
       "  0.007699428591877222,\n",
       "  0.0006205199752002954,\n",
       "  0.11953653395175934,\n",
       "  -0.023542774841189384,\n",
       "  -0.014303517527878284,\n",
       "  -0.0043646786361932755,\n",
       "  -0.032264046370983124,\n",
       "  -0.03918657824397087,\n",
       "  0.018507953733205795,\n",
       "  -0.02532033436000347,\n",
       "  -0.027655793353915215,\n",
       "  0.010381837375462055,\n",
       "  -0.018766842782497406,\n",
       "  0.016907762736082077,\n",
       "  -0.019132107496261597,\n",
       "  0.006080885883420706,\n",
       "  -0.01798929087817669,\n",
       "  -0.005014467053115368,\n",
       "  0.013700234703719616,\n",
       "  0.014498359523713589,\n",
       "  -0.015495549887418747,\n",
       "  -0.004860124085098505,\n",
       "  -0.012145010754466057,\n",
       "  0.0344410203397274,\n",
       "  -0.007196902763098478,\n",
       "  -0.08240099251270294,\n",
       "  0.026839978992938995,\n",
       "  0.0032813968136906624,\n",
       "  -0.005417059641331434,\n",
       "  0.06458242237567902,\n",
       "  -0.020112821832299232,\n",
       "  0.0518941804766655,\n",
       "  -0.04021846130490303,\n",
       "  -0.010533066466450691,\n",
       "  -0.01084721740335226,\n",
       "  -0.012287036515772343,\n",
       "  -0.008074367418885231,\n",
       "  -0.012871037237346172,\n",
       "  -0.0016336132539436221,\n",
       "  -0.00797573197633028,\n",
       "  -0.04307335615158081,\n",
       "  0.01211518980562687,\n",
       "  -0.020710064098238945,\n",
       "  0.023857679218053818,\n",
       "  0.02104961685836315,\n",
       "  -0.017245657742023468,\n",
       "  -0.044767946004867554,\n",
       "  -0.05576431751251221,\n",
       "  -0.0651201531291008,\n",
       "  -0.007689269259572029,\n",
       "  0.01501696277409792,\n",
       "  -0.05913592875003815,\n",
       "  -0.012943895533680916,\n",
       "  0.017868660390377045,\n",
       "  0.0014487255830317736,\n",
       "  0.039570461958646774,\n",
       "  0.011287881061434746,\n",
       "  -0.019787590950727463,\n",
       "  0.016153454780578613,\n",
       "  0.004501901566982269,\n",
       "  -0.04781461879611015,\n",
       "  0.03937285393476486,\n",
       "  -0.01288579124957323,\n",
       "  -0.03096875175833702,\n",
       "  0.008497107774019241,\n",
       "  0.04303837567567825,\n",
       "  -0.02645130455493927,\n",
       "  -0.03370412066578865,\n",
       "  0.026586458086967468,\n",
       "  0.042834389954805374,\n",
       "  0.050606485456228256,\n",
       "  0.022808048874139786,\n",
       "  -0.006047395057976246,\n",
       "  0.011181185953319073,\n",
       "  -0.016186704859137535,\n",
       "  -0.06346990913152695,\n",
       "  -0.06115089729428291,\n",
       "  0.010619327425956726,\n",
       "  -0.06970173865556717,\n",
       "  0.017544768750667572,\n",
       "  0.0007226262823678553,\n",
       "  0.041239842772483826,\n",
       "  -0.012038279324769974,\n",
       "  -0.018596023321151733,\n",
       "  0.007776238955557346,\n",
       "  0.030848901718854904,\n",
       "  -0.009814233519136906,\n",
       "  -0.023294728249311447,\n",
       "  -0.00029706061468459666,\n",
       "  -0.03069630078971386,\n",
       "  0.016353700309991837,\n",
       "  -0.04114333540201187,\n",
       "  -0.033266641199588776,\n",
       "  0.012109179049730301,\n",
       "  -0.051869459450244904,\n",
       "  -0.004117252770811319,\n",
       "  0.014654302038252354,\n",
       "  0.0028220475651323795,\n",
       "  -0.03919754549860954,\n",
       "  -0.004175035282969475,\n",
       "  0.04182624816894531,\n",
       "  -0.027859292924404144,\n",
       "  -0.031746409833431244,\n",
       "  0.030129726976156235,\n",
       "  0.05793086811900139,\n",
       "  -0.020272428169846535,\n",
       "  -0.04239963740110397,\n",
       "  0.0029516774229705334,\n",
       "  0.02768365852534771,\n",
       "  -0.043314129114151,\n",
       "  0.03301611170172691,\n",
       "  0.030194416642189026,\n",
       "  -0.05986650288105011,\n",
       "  0.003509470960125327,\n",
       "  0.007295744493603706,\n",
       "  0.036458928138017654,\n",
       "  0.01645222119987011,\n",
       "  0.014415006153285503,\n",
       "  0.01143702119588852,\n",
       "  0.016910776495933533,\n",
       "  -0.011343585327267647,\n",
       "  -0.029562948271632195,\n",
       "  0.005067824386060238,\n",
       "  0.005913889035582542,\n",
       "  -0.022079216316342354,\n",
       "  0.045017726719379425,\n",
       "  -0.06758962571620941,\n",
       "  -0.06260037422180176,\n",
       "  0.035241883248090744,\n",
       "  0.027005460113286972,\n",
       "  -0.016102472320199013,\n",
       "  0.007343095727264881,\n",
       "  -0.06406409293413162,\n",
       "  0.007124415598809719,\n",
       "  -0.0988832414150238,\n",
       "  -0.054354529827833176,\n",
       "  0.0046100555919110775,\n",
       "  -0.050785813480615616,\n",
       "  -0.13209383189678192,\n",
       "  0.018717940896749496,\n",
       "  0.037849996238946915,\n",
       "  0.023419370874762535,\n",
       "  0.006526838056743145,\n",
       "  0.05243085324764252,\n",
       "  -0.011112730018794537,\n",
       "  -0.009778073988854885,\n",
       "  -0.00028374025714583695,\n",
       "  -0.0011743600480258465,\n",
       "  0.0011922837002202868,\n",
       "  0.022864103317260742,\n",
       "  -0.021886607632040977,\n",
       "  -0.08753067255020142,\n",
       "  -0.0017985019367188215,\n",
       "  0.039427705109119415,\n",
       "  -0.011398105882108212,\n",
       "  0.027587691321969032,\n",
       "  0.04846230521798134,\n",
       "  0.002634401898831129,\n",
       "  0.030369801446795464,\n",
       "  -0.06181355193257332,\n",
       "  0.04014764726161957,\n",
       "  -0.0415302999317646,\n",
       "  0.009435730054974556,\n",
       "  0.03227359801530838,\n",
       "  -0.005515401717275381,\n",
       "  0.016782958060503006,\n",
       "  0.045664045959711075,\n",
       "  0.0075900619849562645,\n",
       "  0.0009028919157572091,\n",
       "  0.029891256242990494,\n",
       "  0.006151494570076466,\n",
       "  0.00401092367246747,\n",
       "  0.00974356196820736,\n",
       "  0.010527004487812519,\n",
       "  0.07055158913135529,\n",
       "  -0.051555801182985306,\n",
       "  -0.038054727017879486,\n",
       "  -0.014112140983343124,\n",
       "  0.07940618693828583,\n",
       "  0.01450576726347208,\n",
       "  0.011962758377194405,\n",
       "  0.0050860303454101086,\n",
       "  0.0014697705628350377,\n",
       "  -0.02584371529519558,\n",
       "  -0.044463541358709335,\n",
       "  -0.008329328149557114,\n",
       "  -0.03663516044616699,\n",
       "  -0.014643860049545765,\n",
       "  -0.03649391233921051,\n",
       "  -0.016238370910286903,\n",
       "  0.023869924247264862,\n",
       "  0.02152514085173607,\n",
       "  -0.05171585455536842,\n",
       "  -0.2288965880870819,\n",
       "  0.10807385295629501,\n",
       "  0.01917129196226597,\n",
       "  0.016753261908888817,\n",
       "  0.013119092211127281,\n",
       "  0.029241574928164482,\n",
       "  -0.005473349243402481,\n",
       "  0.035696011036634445,\n",
       "  0.022129198536276817,\n",
       "  0.011657746508717537,\n",
       "  0.009525755420327187,\n",
       "  0.013918958604335785,\n",
       "  0.05983024090528488,\n",
       "  0.014327618293464184,\n",
       "  0.012286781333386898,\n",
       "  0.012320060282945633,\n",
       "  0.015175123699009418,\n",
       "  0.08810511231422424,\n",
       "  0.006948413792997599,\n",
       "  0.016611196100711823,\n",
       "  -0.004207785706967115,\n",
       "  -0.0012703855754807591,\n",
       "  0.05197809636592865,\n",
       "  -0.012470836751163006,\n",
       "  0.009730374440550804,\n",
       "  -0.059552792459726334,\n",
       "  -0.011270486749708652,\n",
       "  0.04926647990942001,\n",
       "  0.008020555600523949,\n",
       "  -0.03290488198399544,\n",
       "  0.005727914161980152,\n",
       "  -0.00655425013974309,\n",
       "  -0.04186909273266792,\n",
       "  -0.004367484245449305,\n",
       "  0.0010835182620212436,\n",
       "  0.0327637642621994,\n",
       "  0.005611900240182877,\n",
       "  -0.018360842019319534,\n",
       "  -0.05152316763997078,\n",
       "  0.017277181148529053,\n",
       "  -0.01470794714987278,\n",
       "  -0.10620596259832382,\n",
       "  -0.004048609174787998,\n",
       "  -0.005445615854114294,\n",
       "  -0.02757607027888298,\n",
       "  0.016491560265421867,\n",
       "  0.02852058969438076,\n",
       "  -0.03363530710339546,\n",
       "  0.02005797065794468,\n",
       "  0.01651953160762787,\n",
       "  0.07302330434322357,\n",
       "  -0.02534428983926773,\n",
       "  -0.008433238603174686,\n",
       "  0.0017489641904830933,\n",
       "  -0.02989800088107586,\n",
       "  0.035147495567798615,\n",
       "  0.03437107428908348,\n",
       "  0.044491108506917953,\n",
       "  -0.015629149973392487,\n",
       "  -0.019966574385762215,\n",
       "  -0.006030702032148838,\n",
       "  0.03209063410758972,\n",
       "  0.02616051957011223,\n",
       "  0.0028229805175215006,\n",
       "  0.03317323699593544,\n",
       "  -0.030945923179388046,\n",
       "  0.009033484384417534,\n",
       "  0.0315602608025074,\n",
       "  -0.030361944809556007,\n",
       "  0.023290345445275307,\n",
       "  -0.03175268694758415,\n",
       "  -0.011144910007715225,\n",
       "  -0.06224234402179718,\n",
       "  0.013800949789583683,\n",
       "  0.011436271481215954,\n",
       "  0.0043907780200243,\n",
       "  0.010090190917253494,\n",
       "  -0.013956348411738873,\n",
       "  -0.0406193882226944,\n",
       "  -0.031226908788084984,\n",
       "  -0.03223836049437523,\n",
       "  0.01833810657262802,\n",
       "  -0.020906411111354828,\n",
       "  -0.021887492388486862,\n",
       "  0.010989593341946602,\n",
       "  0.015487068332731724,\n",
       "  -0.011643550358712673,\n",
       "  0.008672590367496014,\n",
       "  0.04020673409104347,\n",
       "  0.022197473794221878,\n",
       "  -0.04917604476213455,\n",
       "  0.024314986541867256,\n",
       "  -0.004598389379680157,\n",
       "  0.032267868518829346,\n",
       "  0.017002249136567116,\n",
       "  0.06367824226617813,\n",
       "  0.04533487930893898,\n",
       "  -0.0011845168191939592,\n",
       "  0.013077019713819027,\n",
       "  -0.02013709768652916,\n",
       "  -0.01277145091444254,\n",
       "  -0.06399880349636078,\n",
       "  0.011489042080938816,\n",
       "  0.07266818732023239,\n",
       "  0.0048274206928908825,\n",
       "  0.03358977660536766,\n",
       "  -0.05877098813652992,\n",
       "  0.023248763754963875,\n",
       "  -0.02107386291027069,\n",
       "  -0.055765699595212936,\n",
       "  0.047581516206264496,\n",
       "  0.05712009593844414,\n",
       "  0.0013154424959793687,\n",
       "  0.046363234519958496,\n",
       "  -0.00544540723785758,\n",
       "  0.019611479714512825,\n",
       "  -0.01096935011446476,\n",
       "  0.005725086200982332,\n",
       "  -0.03152109310030937,\n",
       "  -0.05149542912840843,\n",
       "  0.010586092248558998,\n",
       "  0.008784576319158077,\n",
       "  0.015610960312187672,\n",
       "  -0.05583610013127327,\n",
       "  0.0032061724923551083,\n",
       "  -0.035721808671951294,\n",
       "  0.0020327114034444094,\n",
       "  -0.016329573467373848,\n",
       "  0.025844721123576164,\n",
       "  -0.02697901614010334,\n",
       "  0.02803858555853367,\n",
       "  -0.014785834588110447,\n",
       "  0.09081943333148956,\n",
       "  -0.022812580689787865,\n",
       "  0.026330525055527687,\n",
       "  0.023249410092830658,\n",
       "  0.019002923741936684,\n",
       "  0.026568707078695297,\n",
       "  -0.012197636067867279,\n",
       "  -0.03607816621661186,\n",
       "  0.004137495532631874,\n",
       "  -0.0603872574865818,\n",
       "  -0.04832072556018829,\n",
       "  -0.07622052729129791,\n",
       "  0.032743725925683975,\n",
       "  0.060960739850997925,\n",
       "  0.09115619957447052,\n",
       "  -0.004288093186914921,\n",
       "  -0.07019162178039551,\n",
       "  -0.0020135354716330767,\n",
       "  0.01735697314143181,\n",
       "  0.04303852841258049,\n",
       "  0.007107906974852085,\n",
       "  0.07364815473556519,\n",
       "  0.03620007634162903,\n",
       "  -0.005196352023631334,\n",
       "  0.0013151048915460706,\n",
       "  -0.018465153872966766,\n",
       "  -0.0015016018878668547,\n",
       "  -0.029895763844251633,\n",
       "  0.006379893980920315,\n",
       "  0.077790267765522,\n",
       "  -0.02447730489075184,\n",
       "  0.03584738075733185,\n",
       "  0.01905999332666397,\n",
       "  -0.033314622938632965,\n",
       "  0.027116289362311363,\n",
       "  0.013784770853817463,\n",
       "  0.041140832006931305,\n",
       "  -0.02100502885878086,\n",
       "  0.023370439186692238,\n",
       "  -0.03850885480642319,\n",
       "  0.010875359177589417,\n",
       "  -0.023510903120040894,\n",
       "  -0.021395539864897728,\n",
       "  -0.04130227491259575,\n",
       "  0.03542465344071388,\n",
       "  -0.08593258261680603,\n",
       "  -0.0004157029034104198,\n",
       "  -0.04186296835541725],\n",
       " [0.0310748890042305,\n",
       "  0.047027431428432465,\n",
       "  0.0011285803047940135,\n",
       "  0.019084811210632324,\n",
       "  0.015286569483578205,\n",
       "  0.05349554866552353,\n",
       "  0.03078519180417061,\n",
       "  0.03505897894501686,\n",
       "  -0.04774361103773117,\n",
       "  -0.001535180606879294,\n",
       "  -0.05855629965662956,\n",
       "  0.015809321776032448,\n",
       "  0.095534548163414,\n",
       "  -0.010369259864091873,\n",
       "  -0.016687536612153053,\n",
       "  0.028658220544457436,\n",
       "  -0.012754133902490139,\n",
       "  -0.01988569088280201,\n",
       "  0.03576579689979553,\n",
       "  0.0008706343360245228,\n",
       "  -0.0054586222395300865,\n",
       "  -0.0005348835256882012,\n",
       "  0.048817940056324005,\n",
       "  0.06898345798254013,\n",
       "  0.08291739225387573,\n",
       "  -0.017453374341130257,\n",
       "  0.031213056296110153,\n",
       "  -0.009467647410929203,\n",
       "  -0.05698758363723755,\n",
       "  -0.035699352622032166,\n",
       "  -0.02732500061392784,\n",
       "  -0.05287807807326317,\n",
       "  0.001488029956817627,\n",
       "  -0.057201117277145386,\n",
       "  -0.0377781055867672,\n",
       "  0.04935600236058235,\n",
       "  -0.002246898366138339,\n",
       "  -0.03753358870744705,\n",
       "  0.03181125223636627,\n",
       "  0.017099203541874886,\n",
       "  -0.011022258549928665,\n",
       "  -0.009147929027676582,\n",
       "  0.02005493827164173,\n",
       "  0.004895064979791641,\n",
       "  0.014975572936236858,\n",
       "  0.020679902285337448,\n",
       "  -0.00021239501074887812,\n",
       "  -0.019407929852604866,\n",
       "  0.0115180229768157,\n",
       "  -0.020168615505099297,\n",
       "  -0.0383371077477932,\n",
       "  -0.03017648495733738,\n",
       "  0.0051687355153262615,\n",
       "  -0.01675313524901867,\n",
       "  0.041925109922885895,\n",
       "  -0.04445197805762291,\n",
       "  -0.03223662078380585,\n",
       "  0.014806050807237625,\n",
       "  -0.028834665194153786,\n",
       "  0.03214368224143982,\n",
       "  -0.017123742029070854,\n",
       "  -0.007973195053637028,\n",
       "  0.023004967719316483,\n",
       "  0.0010948271956294775,\n",
       "  0.010044191963970661,\n",
       "  -0.006112094037234783,\n",
       "  -0.025416528806090355,\n",
       "  -0.003189780516549945,\n",
       "  -0.012865140102803707,\n",
       "  0.011386341415345669,\n",
       "  -0.07713598757982254,\n",
       "  0.01780105195939541,\n",
       "  -0.034796882420778275,\n",
       "  0.03711995854973793,\n",
       "  -0.04131511598825455,\n",
       "  -0.03505508229136467,\n",
       "  -0.046218957751989365,\n",
       "  0.022183941677212715,\n",
       "  0.012430059723556042,\n",
       "  0.012756074778735638,\n",
       "  -0.0057746050879359245,\n",
       "  -0.057690877467393875,\n",
       "  -0.008135823532938957,\n",
       "  -0.05273832753300667,\n",
       "  -0.026210304349660873,\n",
       "  0.017673883587121964,\n",
       "  0.016150981187820435,\n",
       "  -0.01672295108437538,\n",
       "  0.03140978142619133,\n",
       "  0.040184419602155685,\n",
       "  0.005541383754462004,\n",
       "  -0.0045873555354774,\n",
       "  0.024519970640540123,\n",
       "  0.021990351378917694,\n",
       "  -0.02628673054277897,\n",
       "  -0.015451978892087936,\n",
       "  -0.012537895701825619,\n",
       "  0.004336777608841658,\n",
       "  -0.026112720370292664,\n",
       "  -0.08685898780822754,\n",
       "  -0.019933653995394707,\n",
       "  -0.22366151213645935,\n",
       "  0.0004221511771902442,\n",
       "  -0.038471005856990814,\n",
       "  -0.07148193567991257,\n",
       "  -0.020642848685383797,\n",
       "  0.034831732511520386,\n",
       "  -0.02444242313504219,\n",
       "  0.004101388622075319,\n",
       "  -0.029670262709259987,\n",
       "  -0.02331472933292389,\n",
       "  0.04516654834151268,\n",
       "  0.01602828875184059,\n",
       "  -0.024575019255280495,\n",
       "  -0.00441410718485713,\n",
       "  -0.05389615520834923,\n",
       "  0.005758665967732668,\n",
       "  -0.04910953715443611,\n",
       "  0.03079272247850895,\n",
       "  0.03988945856690407,\n",
       "  -0.02025105431675911,\n",
       "  0.010725925676524639,\n",
       "  -0.010488399304449558,\n",
       "  0.00861386489123106,\n",
       "  0.011163976974785328,\n",
       "  -0.016469165682792664,\n",
       "  0.00025377210113219917,\n",
       "  0.0034656766802072525,\n",
       "  0.008005354553461075,\n",
       "  0.03710120543837547,\n",
       "  0.006869003642350435,\n",
       "  -0.02153702639043331,\n",
       "  0.001724237110465765,\n",
       "  0.053781673312187195,\n",
       "  -0.02298150025308132,\n",
       "  -0.05040048062801361,\n",
       "  0.022860340774059296,\n",
       "  -0.005017117131501436,\n",
       "  0.02475596033036709,\n",
       "  -0.01315749529749155,\n",
       "  0.004615822806954384,\n",
       "  -0.06884147226810455,\n",
       "  0.004166504833847284,\n",
       "  -0.037963978946208954,\n",
       "  -0.04138331115245819,\n",
       "  -0.023045185953378677,\n",
       "  0.0005924705183133483,\n",
       "  0.034081555902957916,\n",
       "  0.05117832496762276,\n",
       "  0.0577806755900383,\n",
       "  0.005966030061244965,\n",
       "  -0.08971919864416122,\n",
       "  0.0915621891617775,\n",
       "  -0.01451591681689024,\n",
       "  -0.007382403593510389,\n",
       "  -0.005928367841988802,\n",
       "  0.01154972892254591,\n",
       "  -0.010658728890120983,\n",
       "  -0.028659215196967125,\n",
       "  -0.0015329491579905152,\n",
       "  0.08303611725568771,\n",
       "  -0.027493951842188835,\n",
       "  0.02192777954041958,\n",
       "  0.03934881091117859,\n",
       "  0.04256020486354828,\n",
       "  0.028053097426891327,\n",
       "  0.00715642748400569,\n",
       "  -0.0190717875957489,\n",
       "  -0.02322528138756752,\n",
       "  -0.022020872682332993,\n",
       "  0.005903154611587524,\n",
       "  -0.028429003432393074,\n",
       "  -0.030740702524781227,\n",
       "  -0.029370302334427834,\n",
       "  -0.0070380778051912785,\n",
       "  0.024469707161188126,\n",
       "  -0.04167282208800316,\n",
       "  0.033828191459178925,\n",
       "  0.04322135075926781,\n",
       "  -0.027112789452075958,\n",
       "  -0.07929697632789612,\n",
       "  0.037184447050094604,\n",
       "  0.03839639574289322,\n",
       "  0.019599951803684235,\n",
       "  0.014779339544475079,\n",
       "  -0.005530701018869877,\n",
       "  -0.031165337190032005,\n",
       "  -0.008669051341712475,\n",
       "  -0.029344046488404274,\n",
       "  0.04490349441766739,\n",
       "  0.043159134685993195,\n",
       "  0.014727907255291939,\n",
       "  0.03948596864938736,\n",
       "  0.002951906993985176,\n",
       "  0.05797558277845383,\n",
       "  0.03881559148430824,\n",
       "  -0.026331307366490364,\n",
       "  -0.015238206833600998,\n",
       "  -0.010809576138854027,\n",
       "  -0.0034857289865612984,\n",
       "  -0.0128331882879138,\n",
       "  -0.029736274853348732,\n",
       "  -0.056768748909235,\n",
       "  0.03708798810839653,\n",
       "  -0.005477528087794781,\n",
       "  -0.04251062870025635,\n",
       "  0.03589143604040146,\n",
       "  0.03924053534865379,\n",
       "  -0.005508721340447664,\n",
       "  -0.0014105313457548618,\n",
       "  0.028970954939723015,\n",
       "  0.012197056785225868,\n",
       "  -0.01271503884345293,\n",
       "  0.022791080176830292,\n",
       "  -0.0048549361526966095,\n",
       "  0.07675470411777496,\n",
       "  0.0839950442314148,\n",
       "  0.03710199519991875,\n",
       "  0.000577170867472887,\n",
       "  0.009825416840612888,\n",
       "  0.07812445610761642,\n",
       "  -0.029205558821558952,\n",
       "  0.03321194648742676,\n",
       "  -0.04924674332141876,\n",
       "  0.007522879634052515,\n",
       "  0.03374158591032028,\n",
       "  0.05060874670743942,\n",
       "  0.029498836025595665,\n",
       "  -0.018407760187983513,\n",
       "  -0.012644029222428799,\n",
       "  -0.0033274812158197165,\n",
       "  -0.035193562507629395,\n",
       "  0.0037357446271926165,\n",
       "  0.05314459279179573,\n",
       "  -0.000863409775774926,\n",
       "  0.002374812727794051,\n",
       "  -0.025376934558153152,\n",
       "  0.006365158595144749,\n",
       "  -0.059137869626283646,\n",
       "  -0.04759683459997177,\n",
       "  0.010696480050683022,\n",
       "  -0.06388995051383972,\n",
       "  0.006918128114193678,\n",
       "  -0.007063115946948528,\n",
       "  -0.006184531841427088,\n",
       "  0.00762907275930047,\n",
       "  0.08631112426519394,\n",
       "  0.036030661314725876,\n",
       "  -0.0041816821321845055,\n",
       "  -0.052336081862449646,\n",
       "  -0.049711283296346664,\n",
       "  -0.02155369520187378,\n",
       "  0.006063206121325493,\n",
       "  0.013242673128843307,\n",
       "  0.033769652247428894,\n",
       "  0.052016761153936386,\n",
       "  0.004805019125342369,\n",
       "  0.05486111715435982,\n",
       "  0.01340003777295351,\n",
       "  0.09298636019229889,\n",
       "  -0.021627992391586304,\n",
       "  -0.01413406990468502,\n",
       "  -0.003405874827876687,\n",
       "  -0.05371725931763649,\n",
       "  -0.002331031486392021,\n",
       "  0.033135466277599335,\n",
       "  0.008722694590687752,\n",
       "  0.013504846021533012,\n",
       "  0.03798490762710571,\n",
       "  -0.0034765794407576323,\n",
       "  -0.006673552095890045,\n",
       "  -0.002310114447027445,\n",
       "  0.010726714506745338,\n",
       "  -0.008872205391526222,\n",
       "  -0.00753570394590497,\n",
       "  -0.008936485275626183,\n",
       "  -0.011765055358409882,\n",
       "  -0.017245542258024216,\n",
       "  -0.08858740329742432,\n",
       "  -0.01812017522752285,\n",
       "  -0.015152211301028728,\n",
       "  -0.046359650790691376,\n",
       "  0.04146406799554825,\n",
       "  -0.051785845309495926,\n",
       "  0.03291706368327141,\n",
       "  -0.04729938134551048,\n",
       "  0.02118815667927265,\n",
       "  -0.03532510995864868,\n",
       "  -0.06956890970468521,\n",
       "  0.011152603663504124,\n",
       "  -0.059769969433546066,\n",
       "  0.024194708094000816,\n",
       "  -0.0014590988866984844,\n",
       "  -0.021728312596678734,\n",
       "  0.06939800083637238,\n",
       "  -0.002110101282596588,\n",
       "  -0.022443562746047974,\n",
       "  -0.010942061431705952,\n",
       "  0.06687133759260178,\n",
       "  0.02624066174030304,\n",
       "  -0.003349883947521448,\n",
       "  -0.0015842851717025042,\n",
       "  0.02930871769785881,\n",
       "  0.0542258583009243,\n",
       "  -0.05188769847154617,\n",
       "  -0.004042189568281174,\n",
       "  0.07580428570508957,\n",
       "  -0.013227877207100391,\n",
       "  -0.07654017955064774,\n",
       "  -0.028564943000674248,\n",
       "  0.007324533071368933,\n",
       "  0.02080616168677807,\n",
       "  0.023564187809824944,\n",
       "  -0.010226870886981487,\n",
       "  -0.00281161000020802,\n",
       "  0.012528525665402412,\n",
       "  0.02818003110587597,\n",
       "  0.045871056616306305,\n",
       "  0.006113000214099884,\n",
       "  0.045377686619758606,\n",
       "  -0.0055465311743319035,\n",
       "  0.018283335492014885,\n",
       "  -0.025544622913002968,\n",
       "  -0.02604418620467186,\n",
       "  -0.0015142436604946852,\n",
       "  0.02069413848221302,\n",
       "  -0.006227363366633654,\n",
       "  0.04271594434976578,\n",
       "  0.046642739325761795,\n",
       "  -0.004151975270360708,\n",
       "  -0.016332410275936127,\n",
       "  0.032933492213487625,\n",
       "  0.0014010797021910548,\n",
       "  -0.0005412779864855111,\n",
       "  0.012809895910322666,\n",
       "  -0.025471607223153114,\n",
       "  -0.03308182209730148,\n",
       "  0.03712534159421921,\n",
       "  0.06258925050497055,\n",
       "  0.009457011707127094,\n",
       "  -0.015847591683268547,\n",
       "  0.014784996397793293,\n",
       "  -2.6646424885257147e-05,\n",
       "  -0.0542323961853981,\n",
       "  -0.048885151743888855,\n",
       "  -0.05133824795484543,\n",
       "  0.01704174093902111,\n",
       "  -0.0461277961730957,\n",
       "  0.017748460173606873,\n",
       "  0.01537015475332737,\n",
       "  0.029693029820919037,\n",
       "  0.0013800120214000344,\n",
       "  0.07818694412708282,\n",
       "  -0.016120670363307,\n",
       "  0.06392785906791687,\n",
       "  -0.0009128825040534139,\n",
       "  0.06317508965730667,\n",
       "  0.0004755281552206725,\n",
       "  0.001061027985997498,\n",
       "  -0.00741594098508358,\n",
       "  -0.026082776486873627,\n",
       "  0.037173159420490265,\n",
       "  -0.025109723210334778,\n",
       "  0.05524466931819916,\n",
       "  -0.029359009116888046,\n",
       "  0.02824547328054905,\n",
       "  0.060843564569950104,\n",
       "  0.05191168561577797,\n",
       "  0.016499673947691917,\n",
       "  -0.06433001160621643,\n",
       "  0.024441538378596306,\n",
       "  -0.024901090189814568,\n",
       "  -0.03145165368914604,\n",
       "  0.04374546930193901,\n",
       "  -0.007950842380523682,\n",
       "  -0.0178897175937891,\n",
       "  -0.007491802331060171,\n",
       "  -0.03203124180436134,\n",
       "  -0.013781906105577946,\n",
       "  0.01383284479379654,\n",
       "  0.0382864736020565,\n",
       "  0.030571235343813896,\n",
       "  0.021218324080109596,\n",
       "  0.025250393897294998,\n",
       "  0.032689858227968216,\n",
       "  0.006633662153035402,\n",
       "  0.04555223137140274,\n",
       "  0.01047514472156763,\n",
       "  0.029611393809318542,\n",
       "  -0.03146401420235634,\n",
       "  -0.06214979663491249,\n",
       "  -0.04404742643237114,\n",
       "  -0.012842421419918537,\n",
       "  -0.033866144716739655,\n",
       "  0.02665151096880436,\n",
       "  0.03610305115580559,\n",
       "  0.018351783975958824,\n",
       "  0.05710381641983986,\n",
       "  -0.02576092630624771,\n",
       "  -0.036920834332704544,\n",
       "  0.006670734379440546,\n",
       "  -0.001670953119173646,\n",
       "  -0.03442397713661194,\n",
       "  -0.015759222209453583,\n",
       "  -0.0068028452806174755,\n",
       "  -0.02007058635354042,\n",
       "  -0.0001325314078712836,\n",
       "  -0.02031264454126358,\n",
       "  0.026491740718483925,\n",
       "  0.002717208117246628,\n",
       "  -0.024259570986032486,\n",
       "  -0.010302438400685787,\n",
       "  0.02581556886434555,\n",
       "  0.06218778342008591,\n",
       "  0.017662780359387398,\n",
       "  -0.026191310957074165,\n",
       "  -0.021925490349531174,\n",
       "  0.010035470128059387,\n",
       "  0.005331419873982668,\n",
       "  -0.053658612072467804,\n",
       "  -0.10217472165822983,\n",
       "  0.020550671964883804,\n",
       "  -0.0021171108819544315,\n",
       "  0.011192703619599342,\n",
       "  0.010045463219285011,\n",
       "  -0.017812339588999748,\n",
       "  0.033365003764629364,\n",
       "  -0.02378433384001255,\n",
       "  -0.009879287332296371,\n",
       "  0.011204179376363754,\n",
       "  -0.0010466681560501456,\n",
       "  0.028070684522390366,\n",
       "  0.01246646512299776,\n",
       "  -0.0587596632540226,\n",
       "  0.01737223193049431,\n",
       "  -0.06207066774368286,\n",
       "  -0.023694608360528946,\n",
       "  0.028883956372737885,\n",
       "  0.05265817418694496,\n",
       "  0.017012232914566994,\n",
       "  -0.023701895028352737,\n",
       "  -0.006449739448726177,\n",
       "  0.005570834968239069,\n",
       "  -0.03581390529870987,\n",
       "  -0.06388692557811737,\n",
       "  -0.019014872610569,\n",
       "  0.007033364847302437,\n",
       "  -0.03331127017736435,\n",
       "  0.026019005104899406,\n",
       "  0.010026453994214535,\n",
       "  0.006509072612971067,\n",
       "  0.0017524815630167723,\n",
       "  -0.03502503037452698,\n",
       "  0.015528869815170765,\n",
       "  0.06207038089632988,\n",
       "  -0.019001774489879608,\n",
       "  0.011379254050552845,\n",
       "  -0.012985140085220337,\n",
       "  0.008586212992668152,\n",
       "  -0.011858182959258556,\n",
       "  0.032530929893255234,\n",
       "  -0.01774631440639496,\n",
       "  0.021248800680041313,\n",
       "  -0.01128473225980997,\n",
       "  -0.017061971127986908,\n",
       "  -0.014554094523191452,\n",
       "  0.011789618991315365,\n",
       "  -0.027750059962272644,\n",
       "  0.030648620799183846,\n",
       "  -0.033436935395002365,\n",
       "  -0.006088341120630503,\n",
       "  -0.027968529611825943,\n",
       "  0.03992486000061035,\n",
       "  -0.08716727048158646,\n",
       "  0.008034718222916126,\n",
       "  0.01773800514638424,\n",
       "  0.06756777316331863,\n",
       "  -0.01964733377099037,\n",
       "  0.007667488884180784,\n",
       "  -0.0032971282489597797,\n",
       "  -0.024206992238759995,\n",
       "  0.009899729862809181,\n",
       "  -0.014986813999712467,\n",
       "  0.02934887446463108,\n",
       "  -0.0017275613499805331,\n",
       "  -0.023603055626153946,\n",
       "  -0.005090087186545134,\n",
       "  -0.019511863589286804,\n",
       "  -0.007258797530084848,\n",
       "  -0.036845553666353226,\n",
       "  0.00879086833447218,\n",
       "  0.015527521260082722,\n",
       "  0.008838572539389133,\n",
       "  -0.046577271074056625,\n",
       "  -0.04010187089443207,\n",
       "  0.04309447854757309,\n",
       "  -0.0065406570211052895,\n",
       "  -0.02940988913178444,\n",
       "  0.0687221810221672,\n",
       "  0.027665868401527405,\n",
       "  -0.013383038341999054,\n",
       "  -0.010238698683679104,\n",
       "  -0.0054470025934278965,\n",
       "  0.03434478119015694,\n",
       "  -0.08374595642089844,\n",
       "  0.019686298444867134,\n",
       "  0.045306675136089325,\n",
       "  -0.0322442501783371,\n",
       "  -0.003958476707339287,\n",
       "  0.037904467433691025,\n",
       "  0.0392175018787384,\n",
       "  -0.0005518848774954677,\n",
       "  -0.03335057199001312,\n",
       "  -0.011340439319610596,\n",
       "  -0.020662900060415268,\n",
       "  -0.026411602273583412,\n",
       "  -0.009385177865624428,\n",
       "  -0.006973168347030878,\n",
       "  0.0026607306208461523,\n",
       "  -0.01521703228354454,\n",
       "  0.04452697932720184,\n",
       "  -0.06833036243915558,\n",
       "  -0.03079148940742016,\n",
       "  0.018672866746783257,\n",
       "  0.0013233674690127373,\n",
       "  -0.009773891419172287,\n",
       "  -0.00862868782132864,\n",
       "  -0.045971788465976715,\n",
       "  0.003291160101071,\n",
       "  -0.03659595921635628,\n",
       "  -0.05236257240176201,\n",
       "  0.012760437093675137,\n",
       "  -0.049056269228458405,\n",
       "  -0.06181939318776131,\n",
       "  0.02066779136657715,\n",
       "  0.022310970351099968,\n",
       "  0.05251428484916687,\n",
       "  -0.008078569546341896,\n",
       "  0.009042487479746342,\n",
       "  -0.023338114842772484,\n",
       "  -0.02318616770207882,\n",
       "  -0.012527809478342533,\n",
       "  0.021764805540442467,\n",
       "  -0.045635756105184555,\n",
       "  0.002608289709314704,\n",
       "  -0.05548654496669769,\n",
       "  -0.04543564096093178,\n",
       "  0.01785278134047985,\n",
       "  0.04018665477633476,\n",
       "  -0.04169580712914467,\n",
       "  -0.00014380495122168213,\n",
       "  0.06422152370214462,\n",
       "  0.0042977710254490376,\n",
       "  0.04628176987171173,\n",
       "  -0.04613988474011421,\n",
       "  0.05458681657910347,\n",
       "  -0.025514695793390274,\n",
       "  0.024937467649579048,\n",
       "  -0.006550068501383066,\n",
       "  0.0005674739368259907,\n",
       "  0.022734159603714943,\n",
       "  0.03130969777703285,\n",
       "  0.0005074148648418486,\n",
       "  -0.024270515888929367,\n",
       "  0.009104296565055847,\n",
       "  -0.01951998472213745,\n",
       "  0.011351218447089195,\n",
       "  0.020558807998895645,\n",
       "  -0.009978147223591805,\n",
       "  0.05358355492353439,\n",
       "  -0.0053101349622011185,\n",
       "  0.025980152189731598,\n",
       "  -0.025918755680322647,\n",
       "  0.03256145119667053,\n",
       "  -0.006648199167102575,\n",
       "  0.02830345183610916,\n",
       "  0.024915896356105804,\n",
       "  -0.019980886951088905,\n",
       "  -0.01630953699350357,\n",
       "  -0.023672979325056076,\n",
       "  -0.009434206411242485,\n",
       "  0.007780877873301506,\n",
       "  -0.05905526131391525,\n",
       "  -0.03351367637515068,\n",
       "  0.010051941499114037,\n",
       "  -0.008523140102624893,\n",
       "  -0.008794196881353855,\n",
       "  -0.00246380059979856,\n",
       "  -0.3283284604549408,\n",
       "  0.12112126499414444,\n",
       "  -0.020919909700751305,\n",
       "  0.0225624181330204,\n",
       "  0.008345324546098709,\n",
       "  0.012673466466367245,\n",
       "  -0.03163187578320503,\n",
       "  0.018084364011883736,\n",
       "  0.020473677664995193,\n",
       "  0.029408743605017662,\n",
       "  0.022757496684789658,\n",
       "  -0.03874586522579193,\n",
       "  0.02744605392217636,\n",
       "  0.01784905418753624,\n",
       "  -0.0748772993683815,\n",
       "  0.02017981931567192,\n",
       "  0.026860306039452553,\n",
       "  0.030989939346909523,\n",
       "  0.037781745195388794,\n",
       "  0.008902921341359615,\n",
       "  0.010204481892287731,\n",
       "  -0.04040852561593056,\n",
       "  0.01264644879847765,\n",
       "  0.006950453855097294,\n",
       "  0.026006892323493958,\n",
       "  -0.024362662807106972,\n",
       "  -0.0007327164057642221,\n",
       "  -0.001677219639532268,\n",
       "  0.005368128418922424,\n",
       "  -0.019112348556518555,\n",
       "  0.023232171311974525,\n",
       "  0.026058664545416832,\n",
       "  0.020822593942284584,\n",
       "  -0.013936666771769524,\n",
       "  0.004522198811173439,\n",
       "  -0.00768006406724453,\n",
       "  -0.00029992515919730067,\n",
       "  0.03437724709510803,\n",
       "  -0.0645255520939827,\n",
       "  0.010485023260116577,\n",
       "  -0.015128707513213158,\n",
       "  -0.006595909129828215,\n",
       "  -0.02554335445165634,\n",
       "  -0.008822844363749027,\n",
       "  0.04848582670092583,\n",
       "  0.06427647173404694,\n",
       "  0.04663419723510742,\n",
       "  -0.04186135157942772,\n",
       "  -0.02497243508696556,\n",
       "  -0.0072257015854120255,\n",
       "  0.0015264825196936727,\n",
       "  -0.03801823779940605,\n",
       "  -0.006067062262445688,\n",
       "  -0.04673927649855614,\n",
       "  -0.010232197120785713,\n",
       "  0.04998921602964401,\n",
       "  0.012667618691921234,\n",
       "  0.018272897228598595,\n",
       "  -0.011522069573402405,\n",
       "  -0.030247341841459274,\n",
       "  0.01034635677933693,\n",
       "  0.01110696792602539,\n",
       "  -0.005301747936755419,\n",
       "  0.012770691886544228,\n",
       "  0.005784902255982161,\n",
       "  0.039058540016412735,\n",
       "  0.03130289912223816,\n",
       "  0.016780640929937363,\n",
       "  -0.0374177023768425,\n",
       "  0.0019885976798832417,\n",
       "  -0.023553920909762383,\n",
       "  0.018609115853905678,\n",
       "  -0.07823997735977173,\n",
       "  0.023405907675623894,\n",
       "  0.0034652571193873882,\n",
       "  -0.028788145631551743,\n",
       "  0.015535489656031132,\n",
       "  -0.02164553664624691,\n",
       "  0.015864428132772446,\n",
       "  0.011425187811255455,\n",
       "  -0.016469305381178856,\n",
       "  0.04037582501769066,\n",
       "  -0.01551708485931158,\n",
       "  -0.03571154922246933,\n",
       "  0.0029404624365270138,\n",
       "  0.017775431275367737,\n",
       "  0.014971839264035225,\n",
       "  0.019906846806406975,\n",
       "  0.023248128592967987,\n",
       "  0.022832578048110008,\n",
       "  -0.013613133691251278,\n",
       "  0.029241926968097687,\n",
       "  0.0344092920422554,\n",
       "  0.006401285529136658,\n",
       "  -0.0115672517567873,\n",
       "  0.05604221671819687,\n",
       "  0.0030947253108024597,\n",
       "  0.00019457745656836778,\n",
       "  -0.017994286492466927,\n",
       "  -0.001816996606066823,\n",
       "  -0.00983000174164772,\n",
       "  0.015120522119104862,\n",
       "  -0.004985480103641748,\n",
       "  0.07639437913894653,\n",
       "  -0.0027959109283983707,\n",
       "  0.0005300502525642514,\n",
       "  -0.09531105309724808,\n",
       "  0.03413022682070732,\n",
       "  0.015879979357123375,\n",
       "  -0.03889842703938484,\n",
       "  0.049433715641498566,\n",
       "  0.024295570328831673,\n",
       "  0.009395980276167393,\n",
       "  -0.008835147134959698,\n",
       "  0.0415862537920475,\n",
       "  -0.010435603559017181,\n",
       "  0.03963327780365944,\n",
       "  -0.03859204798936844,\n",
       "  -0.0368027426302433,\n",
       "  0.009379873983561993,\n",
       "  -0.023633748292922974,\n",
       "  -0.0035239909775555134,\n",
       "  0.004637477453798056,\n",
       "  -0.05510169267654419,\n",
       "  0.020261645317077637,\n",
       "  -0.017840446904301643,\n",
       "  0.020039504393935204,\n",
       "  -0.038043566048145294,\n",
       "  0.04967402294278145,\n",
       "  0.0005629913648590446,\n",
       "  -0.014533746987581253,\n",
       "  -0.014124846085906029,\n",
       "  0.12648078799247742,\n",
       "  -0.026645595207810402,\n",
       "  0.015146581456065178,\n",
       "  0.01758640818297863,\n",
       "  -0.0008427189895883203,\n",
       "  0.013821644708514214,\n",
       "  0.00599935045465827,\n",
       "  0.00845266878604889,\n",
       "  0.009197751991450787,\n",
       "  -0.07344936579465866,\n",
       "  -0.04019888490438461,\n",
       "  0.0017387856496497989,\n",
       "  0.02396298386156559,\n",
       "  0.03737572208046913,\n",
       "  0.10143974423408508,\n",
       "  -0.005554525181651115,\n",
       "  -0.0044077192433178425,\n",
       "  0.006174042820930481,\n",
       "  0.029833493754267693,\n",
       "  -0.00311852665618062,\n",
       "  0.01752088963985443,\n",
       "  0.09172551333904266,\n",
       "  0.03675137460231781,\n",
       "  0.0011277199955657125,\n",
       "  0.04108169302344322,\n",
       "  0.013035308569669724,\n",
       "  -0.034073010087013245,\n",
       "  0.014824346639215946,\n",
       "  -0.023167656734585762,\n",
       "  0.02087354101240635,\n",
       "  0.011987138539552689,\n",
       "  0.038063764572143555,\n",
       "  -0.02195330336689949,\n",
       "  -0.03010672703385353,\n",
       "  0.052907753735780716,\n",
       "  0.02785380743443966,\n",
       "  0.056172922253608704,\n",
       "  -0.014799757860600948,\n",
       "  0.02587762475013733,\n",
       "  -0.03754929453134537,\n",
       "  -0.02685273252427578,\n",
       "  0.008874963037669659,\n",
       "  0.001579742063768208,\n",
       "  -0.050580646842718124,\n",
       "  0.047753024846315384,\n",
       "  -0.055200766772031784,\n",
       "  -0.023469975218176842,\n",
       "  0.004188078921288252]]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用开源向量模型\n",
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "\n",
    "\n",
    "e_model = HuggingFaceEmbeddings(\n",
    "    model_name='maidalun1020/bce-embedding-base_v1',\n",
    "    model_kwargs = {'device': 'cpu'},\n",
    "   encode_kwargs= {'batch_size': 64, 'normalize_embeddings': True}\n",
    ")\n",
    "\n",
    "embedding = e_model.embed_documents( [\n",
    "        \"你好\",\n",
    "        \"你好啊\",\n",
    "        \"你叫什么名字?\",\n",
    "        \"我叫王大锤\",\n",
    "        \"很高兴认识你大锤\",\n",
    "    ])\n",
    "\n",
    "embedding"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## embed_query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-0.014896855689585209,\n",
       " -0.02866695635020733,\n",
       " -0.004045666661113501,\n",
       " 0.05125708505511284,\n",
       " 0.018026502802968025,\n",
       " 0.014191281981766224,\n",
       " 0.007082613650709391,\n",
       " 0.013012683019042015,\n",
       " -0.02069324254989624,\n",
       " 0.014377301558852196,\n",
       " -0.011106605641543865,\n",
       " -0.0006802738644182682,\n",
       " 0.07566133886575699,\n",
       " -0.021810423582792282,\n",
       " -0.024846866726875305,\n",
       " 0.00457024434581399,\n",
       " -0.005529697984457016,\n",
       " -0.0020039905793964863,\n",
       " -0.07116352021694183,\n",
       " -0.055823914706707,\n",
       " -0.013315106742084026,\n",
       " -0.027987653389573097,\n",
       " 0.013466659933328629,\n",
       " -0.009332485496997833,\n",
       " 0.021640513092279434,\n",
       " -0.032848406583070755,\n",
       " -0.03438827767968178,\n",
       " 0.04521971195936203,\n",
       " 0.011514936573803425,\n",
       " 0.07193354517221451,\n",
       " -0.02021225355565548,\n",
       " -0.007915875874459743,\n",
       " -0.05021827667951584,\n",
       " -0.008199954405426979,\n",
       " 0.027046343311667442,\n",
       " -0.01681840978562832,\n",
       " -0.011261120438575745,\n",
       " -0.008717736229300499,\n",
       " -0.028826234862208366,\n",
       " 0.014008275233209133,\n",
       " -0.04365251213312149,\n",
       " 0.03647305443882942,\n",
       " 0.01708937995135784,\n",
       " 0.0049659437499940395,\n",
       " -0.009242343716323376,\n",
       " 0.015925833955407143,\n",
       " 0.03010353446006775,\n",
       " 0.013449838384985924,\n",
       " 0.020536798983812332,\n",
       " 0.02423424832522869,\n",
       " 0.03134752810001373,\n",
       " 0.011908909305930138,\n",
       " -0.036861710250377655,\n",
       " -0.04773825779557228,\n",
       " 0.01248833630234003,\n",
       " -0.10020427405834198,\n",
       " 0.04254273697733879,\n",
       " 0.028220470994710922,\n",
       " -0.004818340763449669,\n",
       " 0.007747553754597902,\n",
       " -0.03298937529325485,\n",
       " 0.03740694373846054,\n",
       " 0.005123123526573181,\n",
       " -0.009881027042865753,\n",
       " 0.006921444088220596,\n",
       " -0.013062087818980217,\n",
       " -0.0070893834345042706,\n",
       " -0.003988280426710844,\n",
       " -0.012386616319417953,\n",
       " -0.020077118650078773,\n",
       " -0.02822744846343994,\n",
       " -0.05187612026929855,\n",
       " 0.006400841288268566,\n",
       " 0.009375546127557755,\n",
       " -0.03176330029964447,\n",
       " 0.012323019094765186,\n",
       " -0.029299773275852203,\n",
       " 0.01460062526166439,\n",
       " 0.008954346179962158,\n",
       " -0.03822513669729233,\n",
       " 0.027664799243211746,\n",
       " 0.025064999237656593,\n",
       " 0.035719599574804306,\n",
       " 0.005127696320414543,\n",
       " -0.023725777864456177,\n",
       " 0.017382141202688217,\n",
       " 0.017874334007501602,\n",
       " -0.023120541125535965,\n",
       " 0.055008452385663986,\n",
       " 0.03135097771883011,\n",
       " 0.007702356204390526,\n",
       " -0.008539426140487194,\n",
       " -0.00952050369232893,\n",
       " -0.029305825009942055,\n",
       " -0.02951236627995968,\n",
       " -0.027193039655685425,\n",
       " 0.02095383033156395,\n",
       " 0.004495423287153244,\n",
       " -0.031579598784446716,\n",
       " 0.00521589582785964,\n",
       " -0.004160947632044554,\n",
       " -0.28786781430244446,\n",
       " 0.030852988362312317,\n",
       " 0.04010258987545967,\n",
       " -0.1064772978425026,\n",
       " -0.005529025103896856,\n",
       " -0.023238079622387886,\n",
       " -0.002402633661404252,\n",
       " -0.010806995443999767,\n",
       " -0.00014934403588995337,\n",
       " -0.05385741963982582,\n",
       " -0.0041848099790513515,\n",
       " 0.05296041816473007,\n",
       " -0.07713460177183151,\n",
       " 0.017134714871644974,\n",
       " -0.04619642347097397,\n",
       " 0.040700092911720276,\n",
       " -0.024314090609550476,\n",
       " -0.012935738079249859,\n",
       " 0.015219696797430515,\n",
       " 0.010876846499741077,\n",
       " 0.03736836090683937,\n",
       " 0.026415949687361717,\n",
       " 0.005175028927624226,\n",
       " 0.01947328820824623,\n",
       " -0.0029314847197383642,\n",
       " 0.0026017376221716404,\n",
       " 0.008864867500960827,\n",
       " -0.01681995764374733,\n",
       " -0.014382272958755493,\n",
       " 0.05092546343803406,\n",
       " -0.029263459146022797,\n",
       " -0.047407303005456924,\n",
       " 0.030651221051812172,\n",
       " -0.018383638933300972,\n",
       " 0.05430403724312782,\n",
       " -0.01910855993628502,\n",
       " -0.0018005217425525188,\n",
       " -0.015578355640172958,\n",
       " 0.02734784223139286,\n",
       " -0.029654545709490776,\n",
       " -0.11777020245790482,\n",
       " 0.0003091396938543767,\n",
       " -0.024114524945616722,\n",
       " -0.03316779062151909,\n",
       " -0.0040845866315066814,\n",
       " -0.006824865471571684,\n",
       " 0.04028601199388504,\n",
       " 0.010051149874925613,\n",
       " -0.08806230127811432,\n",
       " -0.00441765459254384,\n",
       " 0.002149955602362752,\n",
       " 0.10772284120321274,\n",
       " -0.01637749932706356,\n",
       " -0.021246202290058136,\n",
       " 0.026200540363788605,\n",
       " -0.028748586773872375,\n",
       " -0.02950875274837017,\n",
       " 0.01887659728527069,\n",
       " 0.022645462304353714,\n",
       " 0.030411558225750923,\n",
       " 0.006638121325522661,\n",
       " 0.02717147208750248,\n",
       " 0.010636391118168831,\n",
       " 0.045239683240652084,\n",
       " 0.011203094385564327,\n",
       " -0.020715631544589996,\n",
       " -0.017987843602895737,\n",
       " -0.04240932688117027,\n",
       " -0.006708481814712286,\n",
       " 0.0021945396438241005,\n",
       " -0.0042949276976287365,\n",
       " 0.004294675774872303,\n",
       " -0.029810521751642227,\n",
       " -0.003996707499027252,\n",
       " -0.03882967308163643,\n",
       " -0.01742633432149887,\n",
       " 0.025539491325616837,\n",
       " 0.010266401804983616,\n",
       " 0.016089847311377525,\n",
       " -0.03138553351163864,\n",
       " 0.03718609735369682,\n",
       " -0.05279357358813286,\n",
       " 0.007159269880503416,\n",
       " 0.01277988776564598,\n",
       " 0.015629298985004425,\n",
       " -0.02234671451151371,\n",
       " -0.009744515642523766,\n",
       " -0.008271981030702591,\n",
       " -0.0033361241221427917,\n",
       " -0.02191273123025894,\n",
       " 0.05110153183341026,\n",
       " -0.01406919676810503,\n",
       " 0.016459176316857338,\n",
       " 0.03745586425065994,\n",
       " 0.07184956222772598,\n",
       " -0.03875890001654625,\n",
       " -0.02158048003911972,\n",
       " -0.028358591720461845,\n",
       " -0.011528057046234608,\n",
       " 0.03430860862135887,\n",
       " 0.020500296726822853,\n",
       " 0.01941988430917263,\n",
       " 0.014316796325147152,\n",
       " 0.013840116560459137,\n",
       " -0.012532968074083328,\n",
       " 0.01712842471897602,\n",
       " 0.04694126173853874,\n",
       " 0.014643827453255653,\n",
       " 0.052837733179330826,\n",
       " 0.029941851273179054,\n",
       " 0.04467514529824257,\n",
       " -0.045456431806087494,\n",
       " -0.011054914444684982,\n",
       " 0.08245837688446045,\n",
       " -0.08086276054382324,\n",
       " 0.01589977741241455,\n",
       " 0.0019932498689740896,\n",
       " -0.007291282992810011,\n",
       " 0.0017534438520669937,\n",
       " 0.0059894295409321785,\n",
       " -0.05882997810840607,\n",
       " 0.02841554768383503,\n",
       " 0.0004533315950538963,\n",
       " 0.03579843416810036,\n",
       " 0.014925852417945862,\n",
       " -0.0452544167637825,\n",
       " -0.01513769943267107,\n",
       " 0.017525775358080864,\n",
       " -0.0013988778227940202,\n",
       " 0.04600724205374718,\n",
       " 0.0037721029948443174,\n",
       " -0.007594011723995209,\n",
       " -0.027497630566358566,\n",
       " -0.006565819028764963,\n",
       " -0.05393208563327789,\n",
       " 0.021427690982818604,\n",
       " -0.00953997764736414,\n",
       " -0.019462989643216133,\n",
       " -0.06988722831010818,\n",
       " 0.04595154896378517,\n",
       " 0.06602564454078674,\n",
       " 0.017790857702493668,\n",
       " -0.003301422344520688,\n",
       " -0.03675566241145134,\n",
       " -0.009293286129832268,\n",
       " -0.01864638924598694,\n",
       " 0.0001405187213094905,\n",
       " -0.027488484978675842,\n",
       " 0.03920121490955353,\n",
       " -0.030132373794913292,\n",
       " -0.014254550449550152,\n",
       " 0.01468715537339449,\n",
       " 0.005296595860272646,\n",
       " -0.0018563593039289117,\n",
       " -0.0010202714474871755,\n",
       " -0.01377230416983366,\n",
       " 0.009215885773301125,\n",
       " 0.06871525198221207,\n",
       " -0.014977715909481049,\n",
       " -0.031018031761050224,\n",
       " 0.002553265541791916,\n",
       " 0.025742001831531525,\n",
       " -0.003591874847188592,\n",
       " -0.021018659695982933,\n",
       " 0.008855985477566719,\n",
       " 0.05032471567392349,\n",
       " -0.0012972792610526085,\n",
       " 0.06557349115610123,\n",
       " 0.02330668456852436,\n",
       " 0.005616765469312668,\n",
       " 0.006128230132162571,\n",
       " 0.00333358533680439,\n",
       " 0.0730501189827919,\n",
       " -0.0025036856532096863,\n",
       " -0.003699233755469322,\n",
       " -0.000644432962872088,\n",
       " 0.0101335309445858,\n",
       " 0.009210068732500076,\n",
       " -0.08248108625411987,\n",
       " 0.05434023216366768,\n",
       " 0.006835295353084803,\n",
       " -0.001335408422164619,\n",
       " -0.010876243934035301,\n",
       " -0.009347381070256233,\n",
       " 0.052015528082847595,\n",
       " 0.0008977898396551609,\n",
       " 0.017546743154525757,\n",
       " -0.05722226947546005,\n",
       " -0.06076998636126518,\n",
       " 0.015925640240311623,\n",
       " 0.04225381463766098,\n",
       " 0.02246377244591713,\n",
       " -0.014065936207771301,\n",
       " 0.04413430765271187,\n",
       " -0.02991224080324173,\n",
       " 0.0003618369810283184,\n",
       " 0.057404547929763794,\n",
       " 0.015135934576392174,\n",
       " 0.057746924459934235,\n",
       " 0.01906789094209671,\n",
       " 0.050406377762556076,\n",
       " -0.02374236099421978,\n",
       " 0.0664219930768013,\n",
       " 0.0027828682214021683,\n",
       " 0.04833139479160309,\n",
       " 0.030518993735313416,\n",
       " 0.0002141937438864261,\n",
       " -0.015550972893834114,\n",
       " 0.010710436850786209,\n",
       " -0.018982304260134697,\n",
       " -0.023854531347751617,\n",
       " 0.007611151318997145,\n",
       " 0.09565997123718262,\n",
       " 0.03156043961644173,\n",
       " -0.038083311170339584,\n",
       " -0.004045835230499506,\n",
       " 0.08654802292585373,\n",
       " 0.01962098851799965,\n",
       " -0.01711653545498848,\n",
       " 0.0522049255669117,\n",
       " 0.02751437947154045,\n",
       " -0.025262126699090004,\n",
       " 0.06119206175208092,\n",
       " -0.01378672942519188,\n",
       " -0.03841671720147133,\n",
       " 0.018782442435622215,\n",
       " 0.011291944421827793,\n",
       " 0.016717784106731415,\n",
       " -0.002607872011139989,\n",
       " 0.0554177388548851,\n",
       " -0.012719836086034775,\n",
       " -0.004790506325662136,\n",
       " -0.013161682523787022,\n",
       " -0.018235335126519203,\n",
       " 0.043763380497694016,\n",
       " -0.03399438038468361,\n",
       " 0.05584070086479187,\n",
       " 0.12721242010593414,\n",
       " -0.007372491527348757,\n",
       " 0.018151307478547096,\n",
       " -0.015848275274038315,\n",
       " -0.03785606473684311,\n",
       " 0.06928762048482895,\n",
       " -0.006936654914170504,\n",
       " 0.007377458270639181,\n",
       " -0.009859002195298672,\n",
       " 0.009110392071306705,\n",
       " 0.04938522353768349,\n",
       " -0.04123964160680771,\n",
       " 0.05575056001543999,\n",
       " -0.08081391453742981,\n",
       " -0.0002611123491078615,\n",
       " 0.002063767984509468,\n",
       " -0.042671579867601395,\n",
       " -0.031430866569280624,\n",
       " -0.015612823888659477,\n",
       " 0.0037663548719137907,\n",
       " -0.00988785456866026,\n",
       " -0.025348471477627754,\n",
       " 0.003177018603309989,\n",
       " -0.0776929035782814,\n",
       " 0.0028996437322348356,\n",
       " -0.011697260662913322,\n",
       " -0.01382514089345932,\n",
       " 0.0170455202460289,\n",
       " 0.01075578574091196,\n",
       " 0.018016891553997993,\n",
       " -0.022401368245482445,\n",
       " -0.022852115333080292,\n",
       " -0.026071317493915558,\n",
       " -0.0072936792857944965,\n",
       " 0.0008322943467646837,\n",
       " -0.008356746286153793,\n",
       " -0.0341644324362278,\n",
       " 0.009405010379850864,\n",
       " 0.07778383046388626,\n",
       " -0.03898364678025246,\n",
       " 0.04601233825087547,\n",
       " 0.0587235726416111,\n",
       " 0.02564162388443947,\n",
       " 0.07065524160861969,\n",
       " -0.010446349158883095,\n",
       " -0.03592231124639511,\n",
       " -6.165569629956735e-06,\n",
       " -0.011466306634247303,\n",
       " 0.0024278995115309954,\n",
       " -0.03464016690850258,\n",
       " 0.052323535084724426,\n",
       " -0.015534681268036366,\n",
       " 0.08053265511989594,\n",
       " -0.04733068868517876,\n",
       " -0.03590758517384529,\n",
       " -0.02141040936112404,\n",
       " -0.012118619866669178,\n",
       " -0.008605880662798882,\n",
       " -0.01810356415808201,\n",
       " -0.01411269512027502,\n",
       " 0.002860070439055562,\n",
       " -0.04237128049135208,\n",
       " 0.01938454620540142,\n",
       " 0.02971598319709301,\n",
       " -0.009299597702920437,\n",
       " 0.022534320130944252,\n",
       " 0.03140637278556824,\n",
       " 0.04667649418115616,\n",
       " 0.0020980937406420708,\n",
       " -0.00302505143918097,\n",
       " -0.02530292421579361,\n",
       " -0.019364260137081146,\n",
       " -0.018004389479756355,\n",
       " 0.03167024254798889,\n",
       " 0.030429542064666748,\n",
       " 0.03904765099287033,\n",
       " 0.010995994322001934,\n",
       " -0.029847275465726852,\n",
       " -0.006268092431128025,\n",
       " -0.06393670290708542,\n",
       " 0.010328305885195732,\n",
       " -0.043893687427043915,\n",
       " -0.015141280367970467,\n",
       " -0.021500421687960625,\n",
       " -0.0022063099313527346,\n",
       " 0.028901100158691406,\n",
       " 0.017069678753614426,\n",
       " -0.008966833353042603,\n",
       " 0.008670439012348652,\n",
       " -0.0029689748771488667,\n",
       " -0.0036041378043591976,\n",
       " -0.03557717800140381,\n",
       " -0.03722923621535301,\n",
       " 0.014320341870188713,\n",
       " 0.004315335303544998,\n",
       " -0.022689364850521088,\n",
       " 0.05318727344274521,\n",
       " -0.05523884668946266,\n",
       " -0.0017831448931246996,\n",
       " 0.009527435526251793,\n",
       " -0.028933314606547356,\n",
       " 0.03096124157309532,\n",
       " 0.007865806110203266,\n",
       " 0.008754606358706951,\n",
       " 0.05354663357138634,\n",
       " -0.017685089260339737,\n",
       " -0.05934083089232445,\n",
       " -0.010031046345829964,\n",
       " 0.02222738415002823,\n",
       " -0.03275798633694649,\n",
       " 0.046851664781570435,\n",
       " -4.092984818271361e-05,\n",
       " -0.03648125380277634,\n",
       " -0.02375684306025505,\n",
       " -0.020662574097514153,\n",
       " -0.012485208921134472,\n",
       " 0.01921803131699562,\n",
       " -0.003612658241763711,\n",
       " 0.04080653935670853,\n",
       " -0.008821750059723854,\n",
       " -0.012585959397256374,\n",
       " -0.05629926919937134,\n",
       " 0.06117675453424454,\n",
       " 0.07188866287469864,\n",
       " 0.048635464161634445,\n",
       " 0.00093257543630898,\n",
       " -0.01712046191096306,\n",
       " -0.02615654096007347,\n",
       " 0.0061805215664207935,\n",
       " -0.00690509332343936,\n",
       " -0.010685788467526436,\n",
       " -0.013081024400889874,\n",
       " -0.005571465007960796,\n",
       " 0.001609006547369063,\n",
       " -0.029456011950969696,\n",
       " -0.045442916452884674,\n",
       " 0.0009170762496069074,\n",
       " -0.012543557211756706,\n",
       " -0.0035082704853266478,\n",
       " -0.04858655110001564,\n",
       " 0.0493038110435009,\n",
       " -0.027704862877726555,\n",
       " 0.0482989139854908,\n",
       " -0.006588357035070658,\n",
       " -0.024318108335137367,\n",
       " 0.05337747558951378,\n",
       " -0.01659516617655754,\n",
       " -0.002027269219979644,\n",
       " 0.029326241463422775,\n",
       " 0.04247887432575226,\n",
       " 0.04054325073957443,\n",
       " -0.008312865160405636,\n",
       " 0.02150965854525566,\n",
       " 0.005987050477415323,\n",
       " 0.04305712878704071,\n",
       " -0.015144973993301392,\n",
       " 0.01764613389968872,\n",
       " 0.0060737174935638905,\n",
       " -0.038745369762182236,\n",
       " -0.001465108129195869,\n",
       " 0.04750699922442436,\n",
       " -0.0055654519237577915,\n",
       " 0.002070090966299176,\n",
       " -0.041456617414951324,\n",
       " 0.011571299284696579,\n",
       " 0.016157254576683044,\n",
       " -0.00395587831735611,\n",
       " 0.07006017118692398,\n",
       " 0.008080913685262203,\n",
       " -0.0045896414667367935,\n",
       " -0.0017811603611335158,\n",
       " 0.044987138360738754,\n",
       " 0.0018630095291882753,\n",
       " 0.00171413435600698,\n",
       " 0.03169291093945503,\n",
       " 0.0006988142849877477,\n",
       " 0.0038164539728313684,\n",
       " 0.003578100586310029,\n",
       " 0.05875837802886963,\n",
       " 0.01343626994639635,\n",
       " -0.03342672437429428,\n",
       " -0.050098225474357605,\n",
       " -0.06077931448817253,\n",
       " -0.038963980972766876,\n",
       " -0.0852334126830101,\n",
       " 0.024381335824728012,\n",
       " 0.039225492626428604,\n",
       " -0.00814028549939394,\n",
       " -0.03700095787644386,\n",
       " 0.04200299084186554,\n",
       " -0.025010228157043457,\n",
       " 0.032833658158779144,\n",
       " 0.05811091512441635,\n",
       " -0.029285162687301636,\n",
       " 0.037462107837200165,\n",
       " 0.007580910809338093,\n",
       " -0.05828024446964264,\n",
       " 0.01232358068227768,\n",
       " 0.006349744275212288,\n",
       " -0.002161506563425064,\n",
       " 0.02582607790827751,\n",
       " -0.009536460041999817,\n",
       " 0.020620321854948997,\n",
       " 0.010025025345385075,\n",
       " 0.02213822677731514,\n",
       " 0.01779467798769474,\n",
       " -0.011640946380794048,\n",
       " 0.06409876048564911,\n",
       " -0.06671246141195297,\n",
       " -0.00908367894589901,\n",
       " 0.026060353964567184,\n",
       " -0.023127684369683266,\n",
       " 0.028442246839404106,\n",
       " -0.016430547460913658,\n",
       " -0.01433185487985611,\n",
       " 0.030488278716802597,\n",
       " -0.04738202318549156,\n",
       " 0.016316000372171402,\n",
       " -0.03618285804986954,\n",
       " 0.07041609287261963,\n",
       " -0.03891405463218689,\n",
       " 0.018815943971276283,\n",
       " -0.008487701416015625,\n",
       " -0.033374395221471786,\n",
       " 0.002273557474836707,\n",
       " 0.05109364911913872,\n",
       " 0.029848875477910042,\n",
       " 0.017106179147958755,\n",
       " 0.008748694323003292,\n",
       " -0.012354185804724693,\n",
       " 0.006200012285262346,\n",
       " 0.08290603011846542,\n",
       " 0.04192546755075455,\n",
       " 0.021703051403164864,\n",
       " -0.01598374918103218,\n",
       " 0.005861041601747274,\n",
       " 0.0439477376639843,\n",
       " -0.0025376477278769016,\n",
       " 0.024277523159980774,\n",
       " -0.02753482013940811,\n",
       " -0.016611836850643158,\n",
       " 0.027240363880991936,\n",
       " 0.03582683578133583,\n",
       " 0.015569285489618778,\n",
       " -0.011000695638358593,\n",
       " 0.0645940750837326,\n",
       " 0.04388139396905899,\n",
       " -0.013995545916259289,\n",
       " -0.010977520607411861,\n",
       " -0.018626999109983444,\n",
       " -0.2663829028606415,\n",
       " -0.02520248293876648,\n",
       " 0.015605843625962734,\n",
       " 0.016255561262369156,\n",
       " 0.0029343694914132357,\n",
       " 0.030604727566242218,\n",
       " 0.003306675236672163,\n",
       " -0.02212361991405487,\n",
       " 0.008425730280578136,\n",
       " 0.030070528388023376,\n",
       " -0.025876717641949654,\n",
       " -0.010914473794400692,\n",
       " 0.020453408360481262,\n",
       " 0.022759145125746727,\n",
       " -0.04453884810209274,\n",
       " 0.003183321561664343,\n",
       " -0.050899531692266464,\n",
       " 0.00030854574288241565,\n",
       " 0.03674541786313057,\n",
       " 0.031184444203972816,\n",
       " 0.029966238886117935,\n",
       " 0.011775079183280468,\n",
       " 0.018518583849072456,\n",
       " 0.009629488922655582,\n",
       " 0.005018831230700016,\n",
       " -0.03491860628128052,\n",
       " -0.045840922743082047,\n",
       " 0.008916426450014114,\n",
       " 0.02214333973824978,\n",
       " -0.04274474084377289,\n",
       " -0.008157998323440552,\n",
       " 0.012066182680428028,\n",
       " 0.016910823062062263,\n",
       " -0.021183544769883156,\n",
       " -0.014274454675614834,\n",
       " -0.023703565821051598,\n",
       " -0.01241622306406498,\n",
       " -0.03646877780556679,\n",
       " -0.00345408427529037,\n",
       " 0.02083406411111355,\n",
       " -0.002378552919253707,\n",
       " 0.010733461007475853,\n",
       " -0.0504317432641983,\n",
       " 0.00811842642724514,\n",
       " -0.017041530460119247,\n",
       " 0.02468755841255188,\n",
       " 0.0286870039999485,\n",
       " -0.008141876198351383,\n",
       " -0.016640283167362213,\n",
       " -0.032429926097393036,\n",
       " -0.02952326461672783,\n",
       " 0.0020318645983934402,\n",
       " -0.11551674455404282,\n",
       " -0.004432120826095343,\n",
       " 0.007080150302499533,\n",
       " 0.07496962696313858,\n",
       " -0.030094606801867485,\n",
       " -0.014270797371864319,\n",
       " 0.06511198729276657,\n",
       " -0.046765442937612534,\n",
       " 0.04263855144381523,\n",
       " 0.025972574949264526,\n",
       " 0.025784339755773544,\n",
       " 0.019275011494755745,\n",
       " -0.0038532158359885216,\n",
       " -0.05295230448246002,\n",
       " -0.015336894430220127,\n",
       " 0.011271296069025993,\n",
       " -0.0032354218419641256,\n",
       " -0.03610534220933914,\n",
       " 0.024491233751177788,\n",
       " -0.00776550080627203,\n",
       " 0.07542810589075089,\n",
       " -0.056792087852954865,\n",
       " 0.011219051666557789,\n",
       " -0.04237981140613556,\n",
       " 0.008781105279922485,\n",
       " -0.01837221533060074,\n",
       " -0.04474335163831711,\n",
       " 0.025926632806658745,\n",
       " 0.00019716593669727445,\n",
       " -0.00619715778157115,\n",
       " -0.010198325850069523,\n",
       " 0.02789587341248989,\n",
       " -0.022205162793397903,\n",
       " -0.0021294839680194855,\n",
       " 0.049517564475536346,\n",
       " 0.029286226257681847,\n",
       " 0.006545370910316706,\n",
       " -0.0017275429563596845,\n",
       " -0.026890823617577553,\n",
       " -0.04646832495927811,\n",
       " 0.004441417753696442,\n",
       " -0.02445031888782978,\n",
       " 0.0026336275041103363,\n",
       " -0.05616646260023117,\n",
       " 0.04457467421889305,\n",
       " -0.01174844428896904,\n",
       " -0.02116295136511326,\n",
       " 0.023749113082885742,\n",
       " 0.03550298884510994,\n",
       " 0.002395241754129529,\n",
       " 0.0506630502641201,\n",
       " -0.016488639637827873,\n",
       " 0.009194574318826199,\n",
       " -0.029469123110175133,\n",
       " -0.12525536119937897,\n",
       " -0.005460006650537252,\n",
       " 0.0077262576669454575,\n",
       " 0.020240847021341324,\n",
       " -0.05539486184716225,\n",
       " -0.04451466724276543,\n",
       " 0.028858382254838943,\n",
       " 0.00825598556548357,\n",
       " -0.011320352554321289,\n",
       " 0.027098793536424637,\n",
       " 0.0444023534655571,\n",
       " 0.09638048708438873,\n",
       " -0.006389843765646219,\n",
       " -0.025760039687156677,\n",
       " -0.034591883420944214,\n",
       " -0.024251116439700127,\n",
       " -0.025550849735736847,\n",
       " 0.006194756831973791,\n",
       " -0.007557585369795561,\n",
       " 0.010107235983014107,\n",
       " -0.006897598505020142,\n",
       " -0.009011421352624893,\n",
       " -0.0025439413730055094,\n",
       " -0.01501272153109312,\n",
       " 0.04298071935772896,\n",
       " -0.029119137674570084,\n",
       " 0.05838099122047424,\n",
       " 0.010010003112256527,\n",
       " 0.01916234754025936,\n",
       " -0.04397396370768547,\n",
       " -0.03871265426278114,\n",
       " -0.001254840288311243,\n",
       " 0.023700758814811707,\n",
       " 0.02856067754328251,\n",
       " 0.033917710185050964,\n",
       " 0.06417745351791382,\n",
       " -0.030631037428975105,\n",
       " -0.013243349269032478,\n",
       " 0.037612687796354294,\n",
       " -0.03159498795866966,\n",
       " 0.0704866498708725,\n",
       " 0.018143730238080025,\n",
       " 0.005779405124485493,\n",
       " -0.026155535131692886,\n",
       " -0.002975358162075281,\n",
       " -0.014504914171993732,\n",
       " 0.015645597130060196,\n",
       " 0.0787063017487526,\n",
       " 0.01786958798766136,\n",
       " -0.034742191433906555,\n",
       " -0.03794131055474281,\n",
       " -0.010967383161187172,\n",
       " -0.05370030179619789,\n",
       " -0.02979692444205284,\n",
       " 0.03147612884640694,\n",
       " 0.03159356862306595,\n",
       " 0.03127339109778404,\n",
       " 0.01268965657800436,\n",
       " 0.015852157026529312,\n",
       " -0.04798023775219917,\n",
       " -0.04922519251704216,\n",
       " 0.0535561665892601,\n",
       " 0.03163623437285423,\n",
       " -0.0403146930038929,\n",
       " -0.03178035467863083,\n",
       " -0.03627816587686539,\n",
       " 0.05383861064910889,\n",
       " -0.02847464010119438,\n",
       " 0.01967564970254898,\n",
       " -0.027896955609321594,\n",
       " 0.07282786071300507,\n",
       " 0.009340887889266014,\n",
       " 0.022450674325227737,\n",
       " 0.05671684443950653]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embedded_query = e_model.embed_query('这段话体现了什么')\n",
    "\n",
    "embedded_query"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 向量缓存"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 缓存目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.embeddings import CacheBackedEmbeddings\n",
    "from langchain.storage import  LocalFileStore\n",
    "from langchain.document_loaders import TextLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "\n",
    "u_embeddings = HuggingFaceEmbeddings(\n",
    "    model_name='maidalun1020/bce-embedding-base_v1',\n",
    "    model_kwargs = {'device': 'cpu'},\n",
    "   encode_kwargs= {'batch_size': 64, 'normalize_embeddings': True}\n",
    ")\n",
    "\n",
    "fs = LocalFileStore(\"./cache/\")\n",
    "\n",
    "cached_embeddings = CacheBackedEmbeddings.from_bytes_store(\n",
    "    u_embeddings,\n",
    "    fs,\n",
    "    namespace=u_embeddings.model_name,\n",
    ")\n",
    "list(fs.yield_keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:langchain_text_splitters.base:Created a chunk of size 610, which is longer than the specified 600\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[Document(metadata={'source': 'letter.txt'}, page_content='[Generated with ChatGPT]\\n\\nConfidential Document - For Internal Use Only\\n\\nDate: July 1, 2023\\n\\nSubject: Updates and Discussions on Various Topics\\n\\nDear Team,\\n\\nI hope this email finds you well. In this document, I would like to provide you with some important updates and discuss various topics that require our attention. Please treat the information contained herein as highly confidential.'),\n",
       " Document(metadata={'source': 'letter.txt'}, page_content=\"Security and Privacy Measures\\nAs part of our ongoing commitment to ensure the security and privacy of our customers' data, we have implemented robust measures across all our systems. We would like to commend John Doe (email: john.doe@example.com) from the IT department for his diligent work in enhancing our network security. Moving forward, we kindly remind everyone to strictly adhere to our data protection policies and guidelines. Additionally, if you come across any potential security risks or incidents, please report them immediately to our dedicated team at security@example.com.\"),\n",
       " Document(metadata={'source': 'letter.txt'}, page_content='HR Updates and Employee Benefits\\nRecently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: michael.johnson@example.com).'),\n",
       " Document(metadata={'source': 'letter.txt'}, page_content='Marketing Initiatives and Campaigns\\nOur marketing team has been actively working on developing new strategies to increase brand awareness and drive customer engagement. We would like to thank Sarah Thompson (phone: 415-555-1234) for her exceptional efforts in managing our social media platforms. Sarah has successfully increased our follower base by 20% in the past month alone. Moreover, please mark your calendars for the upcoming product launch event on July 15th. We encourage all team members to attend and support this exciting milestone for our company.'),\n",
       " Document(metadata={'source': 'letter.txt'}, page_content=\"Research and Development Projects\\nIn our pursuit of innovation, our research and development department has been working tirelessly on various projects. I would like to acknowledge the exceptional work of David Rodriguez (email: david.rodriguez@example.com) in his role as project lead. David's contributions to the development of our cutting-edge technology have been instrumental. Furthermore, we would like to remind everyone to share their ideas and suggestions for potential new projects during our monthly R&D brainstorming session, scheduled for July 10th.\"),\n",
       " Document(metadata={'source': 'letter.txt'}, page_content=\"Please treat the information in this document with utmost confidentiality and ensure that it is not shared with unauthorized individuals. If you have any questions or concerns regarding the topics discussed, please do not hesitate to reach out to me directly.\\n\\nThank you for your attention, and let's continue to work together to achieve our goals.\\n\\nBest regards,\\n\\nJason Fan\\nCofounder & CEO\\nPsychic\\njason@psychic.dev\")]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加载文档\n",
    "raw_documents = TextLoader('letter.txt').load()\n",
    "# 创建文档分割器\n",
    "text_split = CharacterTextSplitter(chunk_size=600,chunk_overlap=0)\n",
    "documents = text_split.split_documents(raw_documents)\n",
    "documents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.4 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "%timeit -r 1 -n 1 db = FAISS.from_documents(documents,cached_embeddings)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['maidalun1020\\\\bce-embedding-base_v14250f053-4b1e-5c34-927d-a7857749217f',\n",
       " 'maidalun1020\\\\bce-embedding-base_v19286d74c-b3fc-56ff-8b08-9071a193f724',\n",
       " 'maidalun1020\\\\bce-embedding-base_v1b0c54c27-a009-50b4-9ccc-661d5478b195',\n",
       " 'maidalun1020\\\\bce-embedding-base_v1c63ea318-3b5d-533b-960b-46434f8b3c22',\n",
       " 'maidalun1020\\\\bce-embedding-base_v1e94acbbe-7d17-5331-8310-4e37bdc56d31',\n",
       " 'maidalun1020\\\\bce-embedding-base_v1f05b40fb-a095-546e-9c5d-49e069720828']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(fs.yield_keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## chatDoc 文档解析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 读取pdf、excel、doc三种常见的文档格式\n",
    "- 根据文档内容，智能抽取内容并输出相应格式\n",
    "<hr>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3312718990.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[25], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m    pip install docx2txt\u001b[0m\n\u001b[1;37m        ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 需要安装的依赖\n",
    "# pip install docx2txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pypdf\n",
      "  Downloading pypdf-5.1.0-py3-none-any.whl.metadata (7.2 kB)\n",
      "Requirement already satisfied: typing_extensions>=4.0 in c:\\programdata\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages (from pypdf) (4.12.2)\n",
      "Downloading pypdf-5.1.0-py3-none-any.whl (297 kB)\n",
      "Installing collected packages: pypdf\n",
      "Successfully installed pypdf-5.1.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "# pip install pypdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting nltkNote: you may need to restart the kernel to use updated packages.\n",
      "\n",
      "  Using cached nltk-3.9.1-py3-none-any.whl.metadata (2.9 kB)\n",
      "Collecting click (from nltk)\n",
      "  Using cached click-8.1.8-py3-none-any.whl.metadata (2.3 kB)\n",
      "Requirement already satisfied: joblib in c:\\programdata\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages (from nltk) (1.4.2)\n",
      "Requirement already satisfied: regex>=2021.8.3 in c:\\programdata\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages (from nltk) (2024.11.6)\n",
      "Requirement already satisfied: tqdm in c:\\programdata\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages (from nltk) (4.67.1)\n",
      "Requirement already satisfied: colorama in c:\\programdata\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages (from click->nltk) (0.4.6)\n",
      "Using cached nltk-3.9.1-py3-none-any.whl (1.5 MB)\n",
      "Using cached click-8.1.8-py3-none-any.whl (98 kB)\n",
      "Installing collected packages: click, nltk\n",
      "Successfully installed click-8.1.8 nltk-3.9.1\n"
     ]
    }
   ],
   "source": [
    "# pip install nltk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 加载doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(metadata={'source': './file/hbtx.docx'}, page_content='厚板板坯自动化视觉AI——L1接口文档\\n\\n\\n\\n板坯库二切线PLC 电文类型\\n\\n板坯库二切线PLC  (140.24.30.3)\\n\\n板坯库二切线PLC(11)-> 识别系统(20)\\n\\nNo.\\n\\n电文号\\n\\n电文名称\\n\\n触发时刻\\n\\n注释\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n112001\\n\\nA3移载机自动确认 开始/关闭指令\\n\\n开始\\\\结束移动时发送开始\\\\结束算法识别指令\\n\\n\\n\\n\\n\\n112002\\n\\n输入辊道叠钢 开始/关闭指令\\n\\n开始\\\\结束输入辊道轧机时，发送开始\\\\结束算法识别指令\\n\\n\\n\\n\\n\\n112099\\n\\n心跳电文\\n\\n每隔30s触发一次\\n\\n\\n\\n\\n\\n识别系统（20） -> 板坯库二切线PLC(11)\\n\\nNo.\\n\\n电文号\\n\\n电文名称\\n\\n触发时刻\\n\\n注释\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n201101\\n\\nA3移载机自动确认 识别返回\\n\\n算法接收结束指令，停止识别，返回结果\\n\\n\\n\\n\\n\\n201102\\n\\n输入辊道叠钢 识别返回\\n\\n算法接收结束指令，停止识别，返回结果\\n\\n\\n\\n\\n\\n201199\\n\\n心跳电文\\n\\n每隔30s触发一次\\n\\n\\n\\n\\n\\n从板坯库二切线PLC到识别系统的电文内容\\n\\n2.1 A3移载机自动确认 开始/关闭指令\\n\\n主题：L1数据请求112001\\n\\n方向：L1识别系统\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nState\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n指令状态\\n\\n开始\\n\\n结束\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n2.2 输入辊道叠钢 开始/关闭指令\\n\\n主题：L1数据请求112002\\n\\n方向：L1识别系统\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nState\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n指令状态\\n\\n开始\\n\\n结束\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n2.3 心跳电文\\n\\n主题：L1数据请求112099\\n\\n方向：L1识别系统\\n\\n电文长度：30\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n\\n\\nCounter\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n0~9999,每次加一，循环计数\\n\\n\\n\\n\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n\\n从识别系统到板坯库二切线PLC的电文内容\\n\\n\\n\\n\\n\\n3.1 A3移载机自动确认 识别返回\\n\\n主题：识别系统数据请求201101\\n\\n方向：识别系统L1\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nResult\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n识别结果\\n\\n正常\\n\\n异常\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n3.2 输入辊道叠钢 识别返回\\n\\n主题：识别系统数据请求201102\\n\\n方向：识别系统L1\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nResult\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n识别结果\\n\\n正常\\n\\n异常\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n3.3 心跳电文\\n\\n主题：识别系统数据请求201199\\n\\n方向：识别系统L1\\n\\n电文长度：30\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n\\n\\nCounter\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n0~9999,每次加一，循环计数\\n\\n\\n\\n\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n1#加热炉 电文类型\\n\\n1#加热炉 (140.24.30.4)\\n\\n1#加热炉(12)-> 识别系统(20)\\n\\nNo.\\n\\n电文号\\n\\n电文名称\\n\\n触发时刻\\n\\n注释\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n122001\\n\\n装钢偏斜 开始指令\\n\\n抬起钢坯推送前发送指令，算法抓拍图片并识别\\n\\n\\n\\n\\n\\n122002\\n\\n抽钢偏斜 开始指令\\n\\n抽钢后放辊道前发送指令，算法抓拍图片并识别\\n\\n\\n\\n\\n\\n122003\\n\\n炉门状态 开始指令\\n\\n打开炉门结束后发送指令，算法抓拍图片并识别\\n\\n\\n\\n\\n\\n122099\\n\\n心跳电文\\n\\n每隔30s触发一次\\n\\n\\n\\n\\n\\n识别系统（20） -> 1#加热炉（12）\\n\\nNo.\\n\\n电文号\\n\\n电文名称\\n\\n触发时刻\\n\\n注释\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n201201\\n\\n装钢偏斜 识别返回\\n\\n算法抓拍图片并识别完成后，返回结果\\n\\n\\n\\n\\n\\n201202\\n\\n抽钢偏斜 识别返回\\n\\n算法抓拍图片并识别完成后，返回结果\\n\\n\\n\\n\\n\\n201203\\n\\n炉门状态 识别返回\\n\\n算法抓拍图片并识别完成后，返回结果\\n\\n\\n\\n\\n\\n201299\\n\\n心跳电文\\n\\n每隔30s触发一次\\n\\n\\n\\n\\n\\n\\n\\n\\n从1#加热炉PLC到识别系统的电文内容\\n\\n2.1 1#加热炉 装钢偏斜 开始指令\\n\\n主题：L1数据请求122001\\n\\n方向：L1识别系统\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n1、2号炉门\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n2.2 1#加热炉 抽钢偏斜 开始指令\\n\\n主题：L1数据请求122002\\n\\n方向：L1识别系统\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n3、4号炉门\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n2.3 1#加热炉 炉门定位 开始指令\\n\\n主题：L1数据请求122003\\n\\n方向：L1识别系统\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n1、2、3、4号炉门\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n2.4 心跳电文\\n\\n主题：L1数据请求122099\\n\\n方向：L1识别系统\\n\\n电文长度：30\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n\\n\\nCounter\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n0~9999,每次加一，循环计数\\n\\n\\n\\n\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n从识别系统到L1的电文内容\\n\\n3.1 1#加热炉 装钢偏斜 识别返回\\n\\n主题：识别系统数据请求201201\\n\\n方向：识别系统L1\\n\\n电文长度：44\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n1、2号炉门\\n\\n5.\\n\\nResult\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n识别结果\\n\\n正常\\n\\n异常\\n\\n6.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n3.2 1#加热炉 抽钢偏斜 识别返回\\n\\n主题：识别系统数据请求201202\\n\\n方向：识别系统L1\\n\\n电文长度：44\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n3、4号炉门\\n\\n5.\\n\\nResult\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n识别结果\\n\\n正常\\n\\n异常\\n\\n6.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n3.3 1#加热炉 炉门状态 识别返回\\n\\n主题：识别系统数据请求201203\\n\\n方向：识别系统L1\\n\\n电文长度：44\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n1、2、3、4号炉门\\n\\n5.\\n\\nResult\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n识别结果\\n\\n正常\\n\\n异常\\n\\n6.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n3.4 心跳电文\\n\\n主题：识别系统数据请求201299\\n\\n方向：识别系统L1\\n\\n电文长度：30\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n\\n\\nCounter\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n0~9999,每次加一，循环计数\\n\\n\\n\\n\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n2#加热炉 电文类型\\n\\n2#加热炉 (140.24.30.5)\\n\\n2#加热炉(13)-> 识别系统(20)\\n\\nNo.\\n\\n电文号\\n\\n电文名称\\n\\n触发时刻\\n\\n注释\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n1.\\n\\n132001\\n\\n装钢偏斜 开始指令\\n\\n抬起钢坯推送前发送指令，算法抓拍图片并识别\\n\\n\\n\\n2.\\n\\n132002\\n\\n抽钢偏斜 开始指令\\n\\n抽钢后放辊道前发送指令，算法抓拍图片并识别\\n\\n\\n\\n3.\\n\\n132003\\n\\n炉门状态 开始指令\\n\\n打开炉门结束后发送指令，算法抓拍图片并识别\\n\\n\\n\\n4.\\n\\n132099\\n\\n心跳电文\\n\\n每隔30s触发一次\\n\\n\\n\\n\\n\\n识别系统（20） -> 2#加热炉(13)\\n\\nNo.\\n\\n电文号\\n\\n电文名称\\n\\n触发时刻\\n\\n注释\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n201301\\n\\n装钢偏斜 识别返回\\n\\n算法抓拍图片并识别完成后，返回结果\\n\\n\\n\\n\\n\\n201302\\n\\n抽钢偏斜 识别返回\\n\\n算法抓拍图片并识别完成后，返回结果\\n\\n\\n\\n\\n\\n201303\\n\\n炉门状态 识别返回\\n\\n算法抓拍图片并识别完成后，返回结果\\n\\n\\n\\n\\n\\n201399\\n\\n心跳电文\\n\\n每隔30s触发一次\\n\\n\\n\\n\\n\\n\\n\\n\\n从2#加热炉PLC到识别系统的电文内容\\n\\n2.1 2#加热炉 装钢偏斜 开始指令\\n\\n主题：L1数据请求132001\\n\\n方向：L1识别系统\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n1、2号炉门\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n2.2 2#加热炉 抽钢偏斜 开始指令\\n\\n主题：L1数据请求132002\\n\\n方向：L1识别系统\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n3、4号炉门\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n2.3 2#加热炉 炉门定位 开始指令\\n\\n主题：L1数据请求132003\\n\\n方向：L1识别系统\\n\\n电文长度：42\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n1、2、3、4号炉门\\n\\n5.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n2.4 心跳电文\\n\\n主题：L1数据请求132099\\n\\n方向：L1识别系统\\n\\n电文长度：30\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n\\n\\nCounter\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n0~9999,每次加一，循环计数\\n\\n\\n\\n\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n\\n从识别系统到L1的电文内容\\n\\n3.1 2#加热炉 装钢偏斜 识别返回\\n\\n主题：识别系统数据请求201301\\n\\n方向：识别系统L1\\n\\n电文长度：44\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n1、2号炉门\\n\\n5.\\n\\nResult\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n识别结果\\n\\n正常\\n\\n异常\\n\\n6.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n3.2 2#加热炉 抽钢偏斜 识别返回\\n\\n主题：识别系统数据请求201302\\n\\n方向：识别系统L1\\n\\n电文长度：44\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n3、4号炉门\\n\\n5.\\n\\nResult\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n识别结果\\n\\n正常\\n\\n异常\\n\\n6.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n3.3 2#加热炉 炉门状态 识别返回\\n\\n主题：识别系统数据请求201303\\n\\n方向：识别系统L1\\n\\n电文长度：44\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n1.\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n2.\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n3.\\n\\nPlanNo\\n\\nCHAR12\\n\\n--\\n\\n\\n\\n\\n\\n计划板坯号\\n\\nL1下发的板坯号\\n\\n4.\\n\\nDoorNum\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n炉门编号\\n\\n1、2、3、4号炉门\\n\\n5.\\n\\nResult\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n识别结果\\n\\n正常\\n\\n异常\\n\\n6.\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留\\n\\n\\n\\n\\n\\n\\n\\n3.4 心跳电文\\n\\n主题：识别系统数据请求201399\\n\\n方向：识别系统L1\\n\\n电文长度：30\\n\\n编号\\n\\n数据内容\\n\\n类型\\n\\n单位\\n\\n最大值\\n\\n最小值\\n\\n数据说明\\n\\n备注\\n\\n\\n\\nTelLen\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文长度\\n\\n电文头\\n\\n\\n\\nTelID\\n\\nINT16\\n\\n--\\n\\n\\u3000\\n\\n\\u3000\\n\\n电文号\\n\\n电文号\\n\\n\\n\\nCounter\\n\\nINT16\\n\\n--\\n\\n\\n\\n\\n\\n0~9999,每次加一，循环计数\\n\\n\\n\\n\\n\\nSpare\\n\\nCHAR24\\n\\n--\\n\\n\\n\\n\\n\\n预留')]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.document_loaders import Docx2txtLoader\n",
    "\n",
    "class ChatDocx():\n",
    "    def getFile(file_path):\n",
    "        loader = Docx2txtLoader(file_path)\n",
    "        return loader.load()\n",
    "ChatDocx.getFile('./file/hbtx.docx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 加载pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(metadata={'source': './file/gdbs.pdf', 'page': 0}, page_content='准备 java环境 \\n\\xa0\\n创建 mysql容器 \\n initdb.sql \\n mysql8.tar \\n\\xa0\\n apache-tomcat-9.0.58.tar.gz \\ntomcat操作 \\n解压 tomcat压缩包 \\n启动服务： ./apache-tomcat-9.0.58/bin/startup.sh\\n关闭服务： ./apache-tomcat-9.0.58/bin/shutdown.sh\\n查看日志： tail ./apache-tomcat-9.0.58/logs/catalina.out -f\\n访问地址： http://ip:8090/building\\nsudo apt-get install openjdk-8-jdk\\njava --version \\xa0# 检验 jdk 是否安装成功\\nsudo apt-get install redis-server \\xa0# 安装 reids\\n# 加载 mysql8 镜像\\nsudo docker load -i mysql8.tar \\nsudo docker run --name mysql8 \\xa0--restart=always \\xa0 --network host \\xa0 -v \\n/usr/mysql/conf:/etc/mysql/conf.d \\xa0 \\xa0 -v /usr/mysql/data2:/var/lib/mysql \\xa0 -e \\nMYSQL_ROOT_PASSWORD=\"root\" \\xa0 \\xa0-e TZ=Asia/Shanghai \\xa0-d mysql:8.0 --lower-case-\\ntable-names=1\\n# 安装客户端\\nsudo apt install mysql-client-core-5.7\\n# 初始化数据库  \\xa0 注意 < 后为 sql 文件路径  回车后输入密码  root\\nmysql -u root -p < /home/sk/java/mysql-init/initdb.sql')]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.document_loaders import  PyPDFLoader\n",
    "\n",
    "class ChatPdf():\n",
    "    def getFile(file_path):\n",
    "        try:\n",
    "            #读取文件\n",
    "            loader = PyPDFLoader(file_path)\n",
    "            return loader.load()\n",
    "        except Exception as e:\n",
    "            print(f'Error: {e}')\n",
    "            return None\n",
    "\n",
    "ChatPdf.getFile('./file/gdbs.pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 加载excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '良品与不良品', 'page_number': 1, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': '95b706bafcadbc40104f77368d202151'}, page_content='AI视觉质检系统的获取良品与不良品数据接口'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '良品与不良品', 'page_number': 1, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': '43235a1d7a1743125e94492240c51a18'}, page_content='接口地址：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '良品与不良品', 'page_number': 1, 'text_as_html': '<table><tr><td>参数格式：</td><td>Json</td></tr><tr><td>请求方式：</td><td>POST</td></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': '43235a1d7a1743125e94492240c51a18', 'category': 'Table', 'element_id': 'a6efe4c873ddeee53346ebd3eaa9466d'}, page_content='参数格式： Json 请求方式： POST'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '良品与不良品', 'page_number': 1, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': '892136209a4b6156407e7a13260c34d7'}, page_content='请求参数：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '良品与不良品', 'page_number': 1, 'text_as_html': '<table><tr><td>参数中文名</td><td>参数英文名</td><td>类型</td><td>是否必须</td><td>说明</td></tr><tr><td>设备ID</td><td>DeviceID</td><td>String</td><td>是</td><td>固定值：10206</td></tr><tr><td>设备名称</td><td>DeviceName</td><td>String</td><td>是</td><td>AI视觉质检系统</td></tr><tr><td>瓶子批次编号</td><td>batchNumber</td><td>String</td><td>是</td><td>获取指定批次的良品、不良品数量</td></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': '892136209a4b6156407e7a13260c34d7', 'category': 'Table', 'element_id': 'f34dc46fed7cee848f1912e4c65d2546'}, page_content='参数中文名 参数英文名 类型 是否必须 说明 设备ID DeviceID String 是 固定值：10206 设备名称 DeviceName String 是 AI视觉质检系统 瓶子批次编号 batchNumber String 是 获取指定批次的良品、不良品数量'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '良品与不良品', 'page_number': 1, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'df013ef4c8f88fe6982c64e8e4b0fc83'}, page_content='响应参数：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '良品与不良品', 'page_number': 1, 'text_as_html': '<table><tr><td>参数中文名</td><td>参数英文名</td><td>类型</td><td>是否必须</td><td>说明</td></tr><tr><td>状态码</td><td>code</td><td>String</td><td>是</td><td>正常编码：200；除此之外的都是异常编码</td></tr><tr><td>状态信息</td><td>Msg</td><td>String</td><td>否</td><td>SUCCESS，或者异常编码对应的异常信息</td></tr><tr><td>设备ID</td><td>DeviceID</td><td>String</td><td>是</td><td>固定值：10206</td></tr><tr><td>瓶子批次编号</td><td>batchNumber</td><td>String</td><td>是</td><td/></tr><tr><td>良品总数</td><td>goodProducts</td><td>String</td><td>是</td><td/></tr><tr><td>不良品总数</td><td>defectiveProducts</td><td>String</td><td>是</td><td/></tr><tr><td>记录时间</td><td>occurrenceTime</td><td>String</td><td>是</td><td/></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': 'df013ef4c8f88fe6982c64e8e4b0fc83', 'category': 'Table', 'element_id': '0bb82d948d6ecc81fef51a0511009ed2'}, page_content='参数中文名 参数英文名 类型 是否必须 说明 状态码 code String 是 正常编码：200；除此之外的都是异常编码 状态信息 Msg String 否 SUCCESS，或者异常编码对应的异常信息 设备ID DeviceID String 是 固定值：10206 瓶子批次编号 batchNumber String 是 良品总数 goodProducts String 是 不良品总数 defectiveProducts String 是 记录时间 occurrenceTime String 是'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '良品与不良品', 'page_number': 1, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'd9bf22c599577e65f6240c9fd5436586'}, page_content='b_record_summary'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '不良品详细信息', 'page_number': 2, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'e31986f9c0930ed8b302a625e1732a5e'}, page_content='AI视觉质检系统的获取不良品性情数据接口'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '不良品详细信息', 'page_number': 2, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': '1624e49165b42eff6fe8c2ce837d7f05'}, page_content='接口地址：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '不良品详细信息', 'page_number': 2, 'text_as_html': '<table><tr><td>参数格式：</td><td>Json</td></tr><tr><td>请求方式：</td><td>POST</td></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': '1624e49165b42eff6fe8c2ce837d7f05', 'category': 'Table', 'element_id': '584b0fa4bd9c6dbf34250e3ef0208183'}, page_content='参数格式： Json 请求方式： POST'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '不良品详细信息', 'page_number': 2, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': '0cab612f9f99c67807bab6f7c6b642b0'}, page_content='请求参数：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '不良品详细信息', 'page_number': 2, 'text_as_html': '<table><tr><td>参数中文名</td><td>参数英文名</td><td>类型</td><td>是否必须</td><td>说明</td></tr><tr><td>设备ID</td><td>DeviceID</td><td>String</td><td>是</td><td>固定值：10206</td></tr><tr><td>设备名称</td><td>DeviceName</td><td>String</td><td>是</td><td>AI视觉质检系统</td></tr><tr><td>瓶子唯一编号</td><td>bottle_number</td><td>String</td><td>是</td><td>获取指定瓶子的不良性情数据</td></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': '0cab612f9f99c67807bab6f7c6b642b0', 'category': 'Table', 'element_id': '4a65a87b524294fac80aa0cf21014e73'}, page_content='参数中文名 参数英文名 类型 是否必须 说明 设备ID DeviceID String 是 固定值：10206 设备名称 DeviceName String 是 AI视觉质检系统 瓶子唯一编号 bottle_number String 是 获取指定瓶子的不良性情数据'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '不良品详细信息', 'page_number': 2, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'd035ccd6abd1adfbbcd4f71739045b82'}, page_content='响应参数：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '不良品详细信息', 'page_number': 2, 'text_as_html': '<table><tr><td>参数中文名</td><td>参数英文名</td><td>类型</td><td>是否必须</td><td>说明</td></tr><tr><td>状态码</td><td>code</td><td>String</td><td>是</td><td>正常编码：200；除此之外的都是异常编码</td></tr><tr><td>状态信息</td><td>Msg</td><td>String</td><td>否</td><td>SUCCESS，或者异常编码对应的异常信息</td></tr><tr><td>设备ID</td><td>DeviceID</td><td>String</td><td>是</td><td>固定值：10206</td></tr><tr><td>瓶子唯一编号</td><td>bottle_number</td><td>String</td><td>是</td><td/></tr><tr><td>瓶子批次编号</td><td>batchNumber</td><td>String</td><td>是</td><td/></tr><tr><td>瓶子状态</td><td>bottleType</td><td>String</td><td>是</td><td>瓶子状态（1：良品；2：不良品）</td></tr><tr><td>图像地址</td><td>bottleImgUrl</td><td>String</td><td>是</td><td/></tr><tr><td>瓶子细化分类</td><td>bottleClassify</td><td>String</td><td>是</td><td>瓶子细化分类（1：玻屑；2：纤维；3：白块；4：色点；5：浑浊；6：金属屑；21：泡头；22：焦头；23：炭化；24：顶部拉丝）</td></tr><tr><td>记录时间</td><td>occurrenceTime</td><td>String</td><td>是</td><td/></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': 'd035ccd6abd1adfbbcd4f71739045b82', 'category': 'Table', 'element_id': '643036bc6ce296a001ec716a489a3cf4'}, page_content='参数中文名 参数英文名 类型 是否必须 说明 状态码 code String 是 正常编码：200；除此之外的都是异常编码 状态信息 Msg String 否 SUCCESS，或者异常编码对应的异常信息 设备ID DeviceID String 是 固定值：10206 瓶子唯一编号 bottle_number String 是 瓶子批次编号 batchNumber String 是 瓶子状态 bottleType String 是 瓶子状态（1：良品；2：不良品） 图像地址 bottleImgUrl String 是 瓶子细化分类 bottleClassify String 是 瓶子细化分类（1：玻屑；2：纤维；3：白块；4：色点；5：浑浊；6：金属屑；21：泡头；22：焦头；23：炭化；24：顶部拉丝） 记录时间 occurrenceTime String 是'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '不良品详细信息', 'page_number': 2, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'dc4792c4acff86ec33fb631ade0b8a8b'}, page_content='b_inspection_record'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '设备运行状态', 'page_number': 3, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'd182238d1f9407a478d59a754f6fba87'}, page_content='AI视觉质检系统的获取质检设备的状态'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '设备运行状态', 'page_number': 3, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'ffe4982fdd1ad1891426fc57f049b3de'}, page_content='接口地址：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '设备运行状态', 'page_number': 3, 'text_as_html': '<table><tr><td>参数格式：</td><td>Json</td></tr><tr><td>请求方式：</td><td>POST</td></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': 'ffe4982fdd1ad1891426fc57f049b3de', 'category': 'Table', 'element_id': '52710e305d7bc2f141700094087d7276'}, page_content='参数格式： Json 请求方式： POST'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '设备运行状态', 'page_number': 3, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'eb6c710be95a6e5b0f5dff4cdd4b9232'}, page_content='请求参数：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '设备运行状态', 'page_number': 3, 'text_as_html': '<table><tr><td>参数中文名</td><td>参数英文名</td><td>类型</td><td>是否必须</td><td>说明</td></tr><tr><td>设备ID</td><td>DeviceID</td><td>String</td><td>是</td><td>固定值：10206</td></tr><tr><td>设备名称</td><td>DeviceName</td><td>String</td><td>是</td><td>AI视觉质检系统</td></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': 'eb6c710be95a6e5b0f5dff4cdd4b9232', 'category': 'Table', 'element_id': '47d14fc475ae379539287975c3153e46'}, page_content='参数中文名 参数英文名 类型 是否必须 说明 设备ID DeviceID String 是 固定值：10206 设备名称 DeviceName String 是 AI视觉质检系统'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '设备运行状态', 'page_number': 3, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': 'dde3fef400a799665506ef923f80697b'}, page_content='响应参数：'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '设备运行状态', 'page_number': 3, 'text_as_html': '<table><tr><td>参数中文名</td><td>参数英文名</td><td>类型</td><td>是否必须</td><td>说明</td></tr><tr><td>状态码</td><td>code</td><td>String</td><td>是</td><td>正常编码：200；除此之外的都是异常编码</td></tr><tr><td>状态信息</td><td>Msg</td><td>String</td><td>否</td><td>SUCCESS，或者异常编码对应的异常信息</td></tr><tr><td>设备ID</td><td>DeviceID</td><td>String</td><td>是</td><td>固定值：10206</td></tr><tr><td>设备状态</td><td>statusCode</td><td>String</td><td>是</td><td>设备状态（1：正常运行；2：暂停；3：关机；4：故障）</td></tr><tr><td>设备状态详细描述</td><td>statusDescription</td><td>String</td><td>是</td><td/></tr><tr><td>记录时间</td><td>timestamp</td><td>String</td><td>是</td><td/></tr></table>', 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'parent_id': 'dde3fef400a799665506ef923f80697b', 'category': 'Table', 'element_id': 'f22cbe5eb057712951d902d6e8849fc4'}, page_content='参数中文名 参数英文名 类型 是否必须 说明 状态码 code String 是 正常编码：200；除此之外的都是异常编码 状态信息 Msg String 否 SUCCESS，或者异常编码对应的异常信息 设备ID DeviceID String 是 固定值：10206 设备状态 statusCode String 是 设备状态（1：正常运行；2：暂停；3：关机；4：故障） 设备状态详细描述 statusDescription String 是 记录时间 timestamp String 是'),\n",
       " Document(metadata={'source': './file/szrs.xlsx', 'file_directory': './file', 'filename': 'szrs.xlsx', 'last_modified': '2024-09-23T18:41:29', 'page_name': '设备运行状态', 'page_number': 3, 'languages': ['eng', 'vie'], 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'category': 'Title', 'element_id': '1da400879b21a807ccb1007a852f8a81'}, page_content='b_device_status')]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.document_loaders import UnstructuredExcelLoader\n",
    "\n",
    "class ChatExcel():\n",
    "    def getFile(file_path):\n",
    "        try:\n",
    "            #读取文件\n",
    "            loader = UnstructuredExcelLoader(file_path,mode='elements')\n",
    "            return loader.load()\n",
    "        except Exception as e:\n",
    "            print(f'Error: {e}')\n",
    "            return None\n",
    "ChatExcel.getFile('./file/szrs.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 整合优化\n",
    "\n",
    "动态处理三种文档，分割文档\n",
    "\n",
    "    这里使用CharacterTextSplitter进行分割，如果文本中有换行符 \\n，可能导致 CharacterTextSplitter 将整个段落视为单个段落，无法按字符数切分。\n",
    "    CharacterTextSplitter 的默认行为是按句子或段落分割，而不是逐字符硬切。\n",
    "    如果没有检测到合适的切分点，可能会保留整个文本不切分。\n",
    "    CharacterTextSplitter 默认会寻找完整的分隔点（如换行、空格等）。如果未明确指定切分规则，则可能无法切分文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import UnstructuredExcelLoader,PyPDFLoader,Docx2txtLoader\n",
    "from langchain.embeddings import CacheBackedEmbeddings\n",
    "from langchain.storage import  LocalFileStore\n",
    "from langchain.document_loaders import TextLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "from langchain_community.vectorstores import FAISS\n",
    "\n",
    "from langchain_openai import ChatOpenAI\n",
    "# 多重向量检索\n",
    "from langchain.retrievers.multi_query import MultiQueryRetriever\n",
    "# 上下文压缩，降低冗余信息\n",
    "from langchain.retrievers import ContextualCompressionRetriever\n",
    "from langchain.retrievers.document_compressors import LLMChainExtractor\n",
    "# 调用llm模型，对话提高精准度\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain,StuffDocumentsChain\n",
    "from langchain.document_transformers import LongContextReorder\n",
    "\n",
    "\n",
    "class ChatDocx():\n",
    "    def __init__(self):\n",
    "        self.doc=None\n",
    "        self.split_text = []\n",
    "        self.llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "    \n",
    "    #处理文档\n",
    "    def splitSentence(self):\n",
    "        raw_doc = self.getFile()\n",
    "        print(raw_doc)\n",
    "        # 文档分割器\n",
    "        text_split = CharacterTextSplitter(chunk_size=50,chunk_overlap=0,separator=\"，\")\n",
    "        split_text = text_split.split_documents(raw_doc)\n",
    "        print(split_text)\n",
    "        self.split_text = split_text\n",
    "        # %timeit -r 1 -n 1 db = FAISS.from_documents(documents,cached_embeddings)\n",
    "\n",
    "    # 构建并返回向量数据库\n",
    "    def embeddingVectorDB(self):\n",
    "        # 初始化向量模型\n",
    "        u_embeddings = HuggingFaceEmbeddings(\n",
    "            #  model_name='maidalun1020/bce-embedding-base_v1',\n",
    "             model_name='sentence-transformers/all-MiniLM-L6-v2',\n",
    "             model_kwargs = {'device': 'cpu'},\n",
    "             encode_kwargs= {'batch_size': 64, 'normalize_embeddings': True}\n",
    "            )\n",
    "        fs = LocalFileStore(\"./cache/\")\n",
    "\n",
    "        # 创建并使用向量缓存\n",
    "        cached_embeddings = CacheBackedEmbeddings.from_bytes_store(\n",
    "            u_embeddings,\n",
    "            fs,\n",
    "            namespace=u_embeddings.model_name,) \n",
    "        \n",
    "        db = FAISS.from_documents(self.split_text,cached_embeddings) \n",
    "\n",
    "        return db\n",
    "\n",
    "\n",
    "    #获取文件内容\n",
    "    def getFile(self):\n",
    "        doc = self.doc\n",
    "        loaders = {\n",
    "            \"docx\":Docx2txtLoader,\n",
    "            \"pdf\":PyPDFLoader,\n",
    "            \"xlsx\":UnstructuredExcelLoader\n",
    "        }\n",
    "        #获取当前文件的后缀\n",
    "        full_extension = doc.split(\".\")[-1]\n",
    "        loader_class = loaders.get(full_extension)\n",
    "        if loader_class:\n",
    "            try:\n",
    "                loader = loader_class(doc)\n",
    "                return loader.load()\n",
    "            except Exception as e:\n",
    "                print(f'Error: {e}')\n",
    "                return None\n",
    "        else:\n",
    "            print(f'Error: Unsupported file format.')\n",
    "            return None\n",
    "\n",
    "\n",
    "    # 提问并获取相关的文本块------仅是从向量库中找出相关的语句\n",
    "    def askQuestion(self, question):\n",
    "        db = self.embeddingVectorDB()\n",
    "\n",
    "        retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "        doc = retriever.get_relevant_documents(question)\n",
    "        return doc\n",
    "\n",
    "    # 多重向量检索\n",
    "    def askQuestionMulti(self, question):\n",
    "        db = self.embeddingVectorDB()\n",
    "\n",
    "        retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "        \n",
    "        retriever_from_llm = MultiQueryRetriever.from_llm(retriever=retriever,llm=self.llm)\n",
    "        doc = retriever_from_llm.get_relevant_documents(question)\n",
    "        return doc\n",
    "\n",
    "    # 模型寻找相关的文本块\n",
    "    def askAndFindFiles(self, question):\n",
    "        db = self.embeddingVectorDB()\n",
    "        retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "        compress = LLMChainExtractor.from_llm(llm = self.llm)\n",
    "        compressor_retriever = ContextualCompressionRetriever(base_retriever=retriever, base_compressor = compress)\n",
    "        return compressor_retriever.get_relevant_documents(query=question)\n",
    "\n",
    "\n",
    "    # 调用llm模型，传入文本prompt提供对话上下文，并进行对话\n",
    "    def askToLLMWithPrompt(self,question):\n",
    "        reordering = LongContextReorder()\n",
    "        reo_docs = reordering.transform_documents(self.split_text)\n",
    "        print(self.split_text)\n",
    "        # 构建文本prompt\n",
    "        doc_pormpt = PromptTemplate(input_variables=['page_content'], template='{page_content}')\n",
    "        # 其他代码保持不变\n",
    "        stuff_prompt_override = \"\"\"Given this text extracts:\n",
    "        ----------------------------------------\n",
    "        {context}\n",
    "        ----------------------------------------\n",
    "        Please answer the following questions:\n",
    "        {query}\n",
    "        \"\"\"\n",
    "\n",
    "        # 定义llm_prompt\n",
    "        prompt = PromptTemplate(template=stuff_prompt_override, input_variables=['context', 'query'])\n",
    "\n",
    "        # 构建chain\n",
    "        llm_chain = LLMChain(llm=self.llm, prompt=prompt)\n",
    "\n",
    "        # 构建stuff_documents_chain\n",
    "        workChain = StuffDocumentsChain(llm_chain=llm_chain, document_prompt=doc_pormpt, document_variable_name=\"context\")\n",
    "\n",
    "        # 构建问题\n",
    "        result = workChain.run(input_documents=reo_docs, query=question)\n",
    "        print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "#设置下logging查看生成查询\n",
    "import logging\n",
    "logging.basicConfig(level=logging.INFO)\n",
    "logging.getLogger(\"langchain.retrievers.multi_query\").setLevel(logging.DEBUG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:langchain_text_splitters.base:Created a chunk of size 172, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 53, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 129, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 75, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 78, which is longer than the specified 50\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(metadata={'source': './file/fake.docx'}, page_content='一、公司基本信息\\n\\n名称：宏图科技发展有限公司\\n\\n注册地址：江苏省南京市雨花台区软件大道101号\\n\\n成立日期：2011年5月16日\\n\\n法定代表人：李强\\n\\n注册资本：人民币5000万元\\n\\n员工人数：约200人\\n\\n联系电话：025-88888888\\n\\n电子邮箱：info@hongtutech.cn\\n\\n\\n\\n二、财务状况概述\\n\\n截至2023年第一季度，宏图科技发展有限公司财务状况堪忧，具体情况如下：\\n\\n1. 资产总额：人民币1.2亿元，较上年同期下降30%。\\n\\n2. 负债总额：人民币1.8亿元，较上年同期上升50%，资不抵债。\\n\\n3. 营业收入：人民币3000万元，较上年同期下降60%。\\n\\n4. 净利润：亏损人民币800万元，去年同期为盈利人民币200万元。\\n\\n5. 现金流量：公司现金流量紧张，现金及现金等价物余额为人民币500万元，难以支撑日常运营。\\n\\n6. 存货：存货积压严重，库存商品价值约为人民币400万元，大部分产品滞销。\\n\\n7. 应收账款：应收账款高达人民币600万元，回收难度大，坏账准备不足。\\n\\n\\n\\n三、主营业务及市场状况\\n\\n宏图科技发展有限公司主要从事计算机软件的研发与销售。近年来，由于市场竞争加剧、技术更新换代速度快和管理层决策失误等原因，公司主营业务收入持续下降。目前，公司面临的主要问题有：\\n\\n1. 产品同质化严重，缺乏核心竞争力。\\n\\n2. 新产品开发进度缓慢，未能及时抓住市场需求变化。\\n\\n3. 市场营销策略不当，导致市场份额大幅缩水。\\n\\n4. 行业内新兴企业崛起迅速，原有客户流失严重。\\n\\n\\n\\n四、债权债务情况\\n\\n宏图科技发展有限公司目前面临的债务问题严峻，具体情况如下：\\n\\n1. 银行贷款：公司向多家银行贷款总额达人民币1亿元，部分贷款已逾期未还。\\n\\n2. 供应商欠款：因现金流紧张，公司拖欠供应商货款达人民币300万元。\\n\\n3. 员工工资及社保：由于资金链断裂，公司拖欠员工工资及社保费用共计人民币200万元。\\n\\n4. 其他应付款项：包括税费、租赁费用等其他应付款项累计约人民币100万元。\\n\\n\\n\\n五、资产清单\\n\\n宏图科技发展有限公司目前拥有的主要资产包括：\\n\\n1. 固定资产：公司办公用房和设备原值合计人民币800万元，累计折旧约400万元。\\n\\n2. 无形资产：包括软件著作权、专利权等无形资产原值合计人民币300万元。\\n\\n3. 存货资产：存货包括已完成软件产品和半成品，价值约为人民币400万元。\\n\\n4. 应收账款：主要包括对外销售软件的应收账款合计人民币600万元。\\n\\n\\n\\n六、潜在风险及预警\\n\\n1. 经营风险：由于连续亏损，公司可能面临破产清算的风险。\\n\\n2. 债务风险：负债累累，若短期内无法筹措足够资金偿还债务，可能面临诉讼或资产被查封的风险。\\n\\n3. 市场风险：行业竞争加剧和市场需求不明朗，可能导致公司未来业绩继续恶化。\\n\\n4. 法律风险：因未能按时支付债务和相关费用，可能面临相关法律诉讼或处罚。\\n\\n\\n\\n七、结论与建议\\n\\n综上所述，宏图科技发展有限公司目前处于财务困境之中，若无外部资金注入或业务转型成功，短期内难以扭转局势。对于不良资产收购方来说，在考虑收购宏图科技的相关资产前，建议进行深入的尽职调查，并制定详细的风险控制和资产处置方案。同时，在估值时应充分考虑到公司所面临的各种潜在风险和清收难度。\\n\\n\\n\\n报告撰写日期：2023年4月20日')]\n",
      "[Document(metadata={'source': './file/fake.docx'}, page_content='一、公司基本信息\\n\\n名称：宏图科技发展有限公司\\n\\n注册地址：江苏省南京市雨花台区软件大道101号\\n\\n成立日期：2011年5月16日\\n\\n法定代表人：李强\\n\\n注册资本：人民币5000万元\\n\\n员工人数：约200人\\n\\n联系电话：025-88888888\\n\\n电子邮箱：info@hongtutech.cn\\n\\n\\n\\n二、财务状况概述\\n\\n截至2023年第一季度'), Document(metadata={'source': './file/fake.docx'}, page_content='宏图科技发展有限公司财务状况堪忧，具体情况如下：\\n\\n1. 资产总额：人民币1.2亿元'), Document(metadata={'source': './file/fake.docx'}, page_content='较上年同期下降30%。\\n\\n2. 负债总额：人民币1.8亿元，较上年同期上升50%'), Document(metadata={'source': './file/fake.docx'}, page_content='资不抵债。\\n\\n3. 营业收入：人民币3000万元'), Document(metadata={'source': './file/fake.docx'}, page_content='较上年同期下降60%。\\n\\n4. 净利润：亏损人民币800万元'), Document(metadata={'source': './file/fake.docx'}, page_content='去年同期为盈利人民币200万元。\\n\\n5. 现金流量：公司现金流量紧张'), Document(metadata={'source': './file/fake.docx'}, page_content='现金及现金等价物余额为人民币500万元，难以支撑日常运营。\\n\\n6. 存货：存货积压严重'), Document(metadata={'source': './file/fake.docx'}, page_content='库存商品价值约为人民币400万元，大部分产品滞销。\\n\\n7. 应收账款：应收账款高达人民币600万元'), Document(metadata={'source': './file/fake.docx'}, page_content='回收难度大'), Document(metadata={'source': './file/fake.docx'}, page_content='坏账准备不足。\\n\\n\\n\\n三、主营业务及市场状况\\n\\n宏图科技发展有限公司主要从事计算机软件的研发与销售。近年来'), Document(metadata={'source': './file/fake.docx'}, page_content='由于市场竞争加剧、技术更新换代速度快和管理层决策失误等原因，公司主营业务收入持续下降。目前'), Document(metadata={'source': './file/fake.docx'}, page_content='公司面临的主要问题有：\\n\\n1. 产品同质化严重，缺乏核心竞争力。\\n\\n2. 新产品开发进度缓慢'), Document(metadata={'source': './file/fake.docx'}, page_content='未能及时抓住市场需求变化。\\n\\n3. 市场营销策略不当'), Document(metadata={'source': './file/fake.docx'}, page_content='导致市场份额大幅缩水。\\n\\n4. 行业内新兴企业崛起迅速'), Document(metadata={'source': './file/fake.docx'}, page_content='原有客户流失严重。\\n\\n\\n\\n四、债权债务情况\\n\\n宏图科技发展有限公司目前面临的债务问题严峻'), Document(metadata={'source': './file/fake.docx'}, page_content='具体情况如下：\\n\\n1. 银行贷款：公司向多家银行贷款总额达人民币1亿元'), Document(metadata={'source': './file/fake.docx'}, page_content='部分贷款已逾期未还。\\n\\n2. 供应商欠款：因现金流紧张'), Document(metadata={'source': './file/fake.docx'}, page_content='公司拖欠供应商货款达人民币300万元。\\n\\n3. 员工工资及社保：由于资金链断裂'), Document(metadata={'source': './file/fake.docx'}, page_content='公司拖欠员工工资及社保费用共计人民币200万元。\\n\\n4. 其他应付款项：包括税费、租赁费用等其他应付款项累计约人民币100万元。\\n\\n\\n\\n五、资产清单\\n\\n宏图科技发展有限公司目前拥有的主要资产包括：\\n\\n1. 固定资产：公司办公用房和设备原值合计人民币800万元'), Document(metadata={'source': './file/fake.docx'}, page_content='累计折旧约400万元。\\n\\n2. 无形资产：包括软件著作权、专利权等无形资产原值合计人民币300万元。\\n\\n3. 存货资产：存货包括已完成软件产品和半成品'), Document(metadata={'source': './file/fake.docx'}, page_content='价值约为人民币400万元。\\n\\n4. 应收账款：主要包括对外销售软件的应收账款合计人民币600万元。\\n\\n\\n\\n六、潜在风险及预警\\n\\n1. 经营风险：由于连续亏损'), Document(metadata={'source': './file/fake.docx'}, page_content='公司可能面临破产清算的风险。\\n\\n2. 债务风险：负债累累，若短期内无法筹措足够资金偿还债务'), Document(metadata={'source': './file/fake.docx'}, page_content='可能面临诉讼或资产被查封的风险。\\n\\n3. 市场风险：行业竞争加剧和市场需求不明朗'), Document(metadata={'source': './file/fake.docx'}, page_content='可能导致公司未来业绩继续恶化。\\n\\n4. 法律风险：因未能按时支付债务和相关费用'), Document(metadata={'source': './file/fake.docx'}, page_content='可能面临相关法律诉讼或处罚。\\n\\n\\n\\n七、结论与建议\\n\\n综上所述'), Document(metadata={'source': './file/fake.docx'}, page_content='宏图科技发展有限公司目前处于财务困境之中，若无外部资金注入或业务转型成功'), Document(metadata={'source': './file/fake.docx'}, page_content='短期内难以扭转局势。对于不良资产收购方来说，在考虑收购宏图科技的相关资产前，建议进行深入的尽职调查'), Document(metadata={'source': './file/fake.docx'}, page_content='并制定详细的风险控制和资产处置方案。同时'), Document(metadata={'source': './file/fake.docx'}, page_content='在估值时应充分考虑到公司所面临的各种潜在风险和清收难度。\\n\\n\\n\\n报告撰写日期：2023年4月20日')]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:langchain.retrievers.multi_query:Generated queries: ['1. 这家公司的全称是什么？  ', '2. 该企业的正式名称是什么？  ', '3. 这家公司的注册名称是什么？']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[Document(id='14d0c613-26df-44ba-8ab1-e5ac4202e16d', metadata={'source': './file/fake.docx'}, page_content='公司可能面临破产清算的风险。\\n\\n2. 债务风险：负债累累，若短期内无法筹措足够资金偿还债务'),\n",
       " Document(id='b95ec79a-4117-408a-9413-67203b26f680', metadata={'source': './file/fake.docx'}, page_content='公司面临的主要问题有：\\n\\n1. 产品同质化严重，缺乏核心竞争力。\\n\\n2. 新产品开发进度缓慢'),\n",
       " Document(id='52645003-3ace-4d6f-b848-14d304219dc3', metadata={'source': './file/fake.docx'}, page_content='可能导致公司未来业绩继续恶化。\\n\\n4. 法律风险：因未能按时支付债务和相关费用'),\n",
       " Document(id='d57fcfe0-1a91-4197-9405-10c2ff6e721e', metadata={'source': './file/fake.docx'}, page_content='公司拖欠供应商货款达人民币300万元。\\n\\n3. 员工工资及社保：由于资金链断裂'),\n",
       " Document(id='3b5390fd-3917-414f-a8c7-8d6fff8fb8f0', metadata={'source': './file/fake.docx'}, page_content='具体情况如下：\\n\\n1. 银行贷款：公司向多家银行贷款总额达人民币1亿元')]"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chatDocx = ChatDocx()\n",
    "chatDocx.doc='./file/fake.docx'\n",
    "chatDocx.splitSentence()\n",
    "chatDocx.split_text\n",
    "chatDocx.embeddingVectorDB()\n",
    "\n",
    "doc = chatDocx.askQuestionMulti(\"公司名称是什么\")\n",
    "doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(metadata={'source': './file/fake.docx'}, page_content='负债累累'),\n",
       " Document(metadata={'source': './file/fake.docx'}, page_content='公司拖欠供应商货款达人民币300万元。')]"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "doc = chatDocx.askAndFindFiles(\"这家公司负债多少？\")\n",
    "doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:langchain_text_splitters.base:Created a chunk of size 172, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 53, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 129, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 75, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 78, which is longer than the specified 50\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(metadata={'source': './file/fake.docx'}, page_content='一、公司基本信息\\n\\n名称：宏图科技发展有限公司\\n\\n注册地址：江苏省南京市雨花台区软件大道101号\\n\\n成立日期：2011年5月16日\\n\\n法定代表人：李强\\n\\n注册资本：人民币5000万元\\n\\n员工人数：约200人\\n\\n联系电话：025-88888888\\n\\n电子邮箱：info@hongtutech.cn\\n\\n\\n\\n二、财务状况概述\\n\\n截至2023年第一季度，宏图科技发展有限公司财务状况堪忧，具体情况如下：\\n\\n1. 资产总额：人民币1.2亿元，较上年同期下降30%。\\n\\n2. 负债总额：人民币1.8亿元，较上年同期上升50%，资不抵债。\\n\\n3. 营业收入：人民币3000万元，较上年同期下降60%。\\n\\n4. 净利润：亏损人民币800万元，去年同期为盈利人民币200万元。\\n\\n5. 现金流量：公司现金流量紧张，现金及现金等价物余额为人民币500万元，难以支撑日常运营。\\n\\n6. 存货：存货积压严重，库存商品价值约为人民币400万元，大部分产品滞销。\\n\\n7. 应收账款：应收账款高达人民币600万元，回收难度大，坏账准备不足。\\n\\n\\n\\n三、主营业务及市场状况\\n\\n宏图科技发展有限公司主要从事计算机软件的研发与销售。近年来，由于市场竞争加剧、技术更新换代速度快和管理层决策失误等原因，公司主营业务收入持续下降。目前，公司面临的主要问题有：\\n\\n1. 产品同质化严重，缺乏核心竞争力。\\n\\n2. 新产品开发进度缓慢，未能及时抓住市场需求变化。\\n\\n3. 市场营销策略不当，导致市场份额大幅缩水。\\n\\n4. 行业内新兴企业崛起迅速，原有客户流失严重。\\n\\n\\n\\n四、债权债务情况\\n\\n宏图科技发展有限公司目前面临的债务问题严峻，具体情况如下：\\n\\n1. 银行贷款：公司向多家银行贷款总额达人民币1亿元，部分贷款已逾期未还。\\n\\n2. 供应商欠款：因现金流紧张，公司拖欠供应商货款达人民币300万元。\\n\\n3. 员工工资及社保：由于资金链断裂，公司拖欠员工工资及社保费用共计人民币200万元。\\n\\n4. 其他应付款项：包括税费、租赁费用等其他应付款项累计约人民币100万元。\\n\\n\\n\\n五、资产清单\\n\\n宏图科技发展有限公司目前拥有的主要资产包括：\\n\\n1. 固定资产：公司办公用房和设备原值合计人民币800万元，累计折旧约400万元。\\n\\n2. 无形资产：包括软件著作权、专利权等无形资产原值合计人民币300万元。\\n\\n3. 存货资产：存货包括已完成软件产品和半成品，价值约为人民币400万元。\\n\\n4. 应收账款：主要包括对外销售软件的应收账款合计人民币600万元。\\n\\n\\n\\n六、潜在风险及预警\\n\\n1. 经营风险：由于连续亏损，公司可能面临破产清算的风险。\\n\\n2. 债务风险：负债累累，若短期内无法筹措足够资金偿还债务，可能面临诉讼或资产被查封的风险。\\n\\n3. 市场风险：行业竞争加剧和市场需求不明朗，可能导致公司未来业绩继续恶化。\\n\\n4. 法律风险：因未能按时支付债务和相关费用，可能面临相关法律诉讼或处罚。\\n\\n\\n\\n七、结论与建议\\n\\n综上所述，宏图科技发展有限公司目前处于财务困境之中，若无外部资金注入或业务转型成功，短期内难以扭转局势。对于不良资产收购方来说，在考虑收购宏图科技的相关资产前，建议进行深入的尽职调查，并制定详细的风险控制和资产处置方案。同时，在估值时应充分考虑到公司所面临的各种潜在风险和清收难度。\\n\\n\\n\\n报告撰写日期：2023年4月20日')]\n",
      "[Document(metadata={'source': './file/fake.docx'}, page_content='一、公司基本信息\\n\\n名称：宏图科技发展有限公司\\n\\n注册地址：江苏省南京市雨花台区软件大道101号\\n\\n成立日期：2011年5月16日\\n\\n法定代表人：李强\\n\\n注册资本：人民币5000万元\\n\\n员工人数：约200人\\n\\n联系电话：025-88888888\\n\\n电子邮箱：info@hongtutech.cn\\n\\n\\n\\n二、财务状况概述\\n\\n截至2023年第一季度'), Document(metadata={'source': './file/fake.docx'}, page_content='宏图科技发展有限公司财务状况堪忧，具体情况如下：\\n\\n1. 资产总额：人民币1.2亿元'), Document(metadata={'source': './file/fake.docx'}, page_content='较上年同期下降30%。\\n\\n2. 负债总额：人民币1.8亿元，较上年同期上升50%'), Document(metadata={'source': './file/fake.docx'}, page_content='资不抵债。\\n\\n3. 营业收入：人民币3000万元'), Document(metadata={'source': './file/fake.docx'}, page_content='较上年同期下降60%。\\n\\n4. 净利润：亏损人民币800万元'), Document(metadata={'source': './file/fake.docx'}, page_content='去年同期为盈利人民币200万元。\\n\\n5. 现金流量：公司现金流量紧张'), Document(metadata={'source': './file/fake.docx'}, page_content='现金及现金等价物余额为人民币500万元，难以支撑日常运营。\\n\\n6. 存货：存货积压严重'), Document(metadata={'source': './file/fake.docx'}, page_content='库存商品价值约为人民币400万元，大部分产品滞销。\\n\\n7. 应收账款：应收账款高达人民币600万元'), Document(metadata={'source': './file/fake.docx'}, page_content='回收难度大'), Document(metadata={'source': './file/fake.docx'}, page_content='坏账准备不足。\\n\\n\\n\\n三、主营业务及市场状况\\n\\n宏图科技发展有限公司主要从事计算机软件的研发与销售。近年来'), Document(metadata={'source': './file/fake.docx'}, page_content='由于市场竞争加剧、技术更新换代速度快和管理层决策失误等原因，公司主营业务收入持续下降。目前'), Document(metadata={'source': './file/fake.docx'}, page_content='公司面临的主要问题有：\\n\\n1. 产品同质化严重，缺乏核心竞争力。\\n\\n2. 新产品开发进度缓慢'), Document(metadata={'source': './file/fake.docx'}, page_content='未能及时抓住市场需求变化。\\n\\n3. 市场营销策略不当'), Document(metadata={'source': './file/fake.docx'}, page_content='导致市场份额大幅缩水。\\n\\n4. 行业内新兴企业崛起迅速'), Document(metadata={'source': './file/fake.docx'}, page_content='原有客户流失严重。\\n\\n\\n\\n四、债权债务情况\\n\\n宏图科技发展有限公司目前面临的债务问题严峻'), Document(metadata={'source': './file/fake.docx'}, page_content='具体情况如下：\\n\\n1. 银行贷款：公司向多家银行贷款总额达人民币1亿元'), Document(metadata={'source': './file/fake.docx'}, page_content='部分贷款已逾期未还。\\n\\n2. 供应商欠款：因现金流紧张'), Document(metadata={'source': './file/fake.docx'}, page_content='公司拖欠供应商货款达人民币300万元。\\n\\n3. 员工工资及社保：由于资金链断裂'), Document(metadata={'source': './file/fake.docx'}, page_content='公司拖欠员工工资及社保费用共计人民币200万元。\\n\\n4. 其他应付款项：包括税费、租赁费用等其他应付款项累计约人民币100万元。\\n\\n\\n\\n五、资产清单\\n\\n宏图科技发展有限公司目前拥有的主要资产包括：\\n\\n1. 固定资产：公司办公用房和设备原值合计人民币800万元'), Document(metadata={'source': './file/fake.docx'}, page_content='累计折旧约400万元。\\n\\n2. 无形资产：包括软件著作权、专利权等无形资产原值合计人民币300万元。\\n\\n3. 存货资产：存货包括已完成软件产品和半成品'), Document(metadata={'source': './file/fake.docx'}, page_content='价值约为人民币400万元。\\n\\n4. 应收账款：主要包括对外销售软件的应收账款合计人民币600万元。\\n\\n\\n\\n六、潜在风险及预警\\n\\n1. 经营风险：由于连续亏损'), Document(metadata={'source': './file/fake.docx'}, page_content='公司可能面临破产清算的风险。\\n\\n2. 债务风险：负债累累，若短期内无法筹措足够资金偿还债务'), Document(metadata={'source': './file/fake.docx'}, page_content='可能面临诉讼或资产被查封的风险。\\n\\n3. 市场风险：行业竞争加剧和市场需求不明朗'), Document(metadata={'source': './file/fake.docx'}, page_content='可能导致公司未来业绩继续恶化。\\n\\n4. 法律风险：因未能按时支付债务和相关费用'), Document(metadata={'source': './file/fake.docx'}, page_content='可能面临相关法律诉讼或处罚。\\n\\n\\n\\n七、结论与建议\\n\\n综上所述'), Document(metadata={'source': './file/fake.docx'}, page_content='宏图科技发展有限公司目前处于财务困境之中，若无外部资金注入或业务转型成功'), Document(metadata={'source': './file/fake.docx'}, page_content='短期内难以扭转局势。对于不良资产收购方来说，在考虑收购宏图科技的相关资产前，建议进行深入的尽职调查'), Document(metadata={'source': './file/fake.docx'}, page_content='并制定详细的风险控制和资产处置方案。同时'), Document(metadata={'source': './file/fake.docx'}, page_content='在估值时应充分考虑到公司所面临的各种潜在风险和清收难度。\\n\\n\\n\\n报告撰写日期：2023年4月20日')]\n",
      "[Document(metadata={'source': './file/fake.docx'}, page_content='一、公司基本信息\\n\\n名称：宏图科技发展有限公司\\n\\n注册地址：江苏省南京市雨花台区软件大道101号\\n\\n成立日期：2011年5月16日\\n\\n法定代表人：李强\\n\\n注册资本：人民币5000万元\\n\\n员工人数：约200人\\n\\n联系电话：025-88888888\\n\\n电子邮箱：info@hongtutech.cn\\n\\n\\n\\n二、财务状况概述\\n\\n截至2023年第一季度'), Document(metadata={'source': './file/fake.docx'}, page_content='宏图科技发展有限公司财务状况堪忧，具体情况如下：\\n\\n1. 资产总额：人民币1.2亿元'), Document(metadata={'source': './file/fake.docx'}, page_content='较上年同期下降30%。\\n\\n2. 负债总额：人民币1.8亿元，较上年同期上升50%'), Document(metadata={'source': './file/fake.docx'}, page_content='资不抵债。\\n\\n3. 营业收入：人民币3000万元'), Document(metadata={'source': './file/fake.docx'}, page_content='较上年同期下降60%。\\n\\n4. 净利润：亏损人民币800万元'), Document(metadata={'source': './file/fake.docx'}, page_content='去年同期为盈利人民币200万元。\\n\\n5. 现金流量：公司现金流量紧张'), Document(metadata={'source': './file/fake.docx'}, page_content='现金及现金等价物余额为人民币500万元，难以支撑日常运营。\\n\\n6. 存货：存货积压严重'), Document(metadata={'source': './file/fake.docx'}, page_content='库存商品价值约为人民币400万元，大部分产品滞销。\\n\\n7. 应收账款：应收账款高达人民币600万元'), Document(metadata={'source': './file/fake.docx'}, page_content='回收难度大'), Document(metadata={'source': './file/fake.docx'}, page_content='坏账准备不足。\\n\\n\\n\\n三、主营业务及市场状况\\n\\n宏图科技发展有限公司主要从事计算机软件的研发与销售。近年来'), Document(metadata={'source': './file/fake.docx'}, page_content='由于市场竞争加剧、技术更新换代速度快和管理层决策失误等原因，公司主营业务收入持续下降。目前'), Document(metadata={'source': './file/fake.docx'}, page_content='公司面临的主要问题有：\\n\\n1. 产品同质化严重，缺乏核心竞争力。\\n\\n2. 新产品开发进度缓慢'), Document(metadata={'source': './file/fake.docx'}, page_content='未能及时抓住市场需求变化。\\n\\n3. 市场营销策略不当'), Document(metadata={'source': './file/fake.docx'}, page_content='导致市场份额大幅缩水。\\n\\n4. 行业内新兴企业崛起迅速'), Document(metadata={'source': './file/fake.docx'}, page_content='原有客户流失严重。\\n\\n\\n\\n四、债权债务情况\\n\\n宏图科技发展有限公司目前面临的债务问题严峻'), Document(metadata={'source': './file/fake.docx'}, page_content='具体情况如下：\\n\\n1. 银行贷款：公司向多家银行贷款总额达人民币1亿元'), Document(metadata={'source': './file/fake.docx'}, page_content='部分贷款已逾期未还。\\n\\n2. 供应商欠款：因现金流紧张'), Document(metadata={'source': './file/fake.docx'}, page_content='公司拖欠供应商货款达人民币300万元。\\n\\n3. 员工工资及社保：由于资金链断裂'), Document(metadata={'source': './file/fake.docx'}, page_content='公司拖欠员工工资及社保费用共计人民币200万元。\\n\\n4. 其他应付款项：包括税费、租赁费用等其他应付款项累计约人民币100万元。\\n\\n\\n\\n五、资产清单\\n\\n宏图科技发展有限公司目前拥有的主要资产包括：\\n\\n1. 固定资产：公司办公用房和设备原值合计人民币800万元'), Document(metadata={'source': './file/fake.docx'}, page_content='累计折旧约400万元。\\n\\n2. 无形资产：包括软件著作权、专利权等无形资产原值合计人民币300万元。\\n\\n3. 存货资产：存货包括已完成软件产品和半成品'), Document(metadata={'source': './file/fake.docx'}, page_content='价值约为人民币400万元。\\n\\n4. 应收账款：主要包括对外销售软件的应收账款合计人民币600万元。\\n\\n\\n\\n六、潜在风险及预警\\n\\n1. 经营风险：由于连续亏损'), Document(metadata={'source': './file/fake.docx'}, page_content='公司可能面临破产清算的风险。\\n\\n2. 债务风险：负债累累，若短期内无法筹措足够资金偿还债务'), Document(metadata={'source': './file/fake.docx'}, page_content='可能面临诉讼或资产被查封的风险。\\n\\n3. 市场风险：行业竞争加剧和市场需求不明朗'), Document(metadata={'source': './file/fake.docx'}, page_content='可能导致公司未来业绩继续恶化。\\n\\n4. 法律风险：因未能按时支付债务和相关费用'), Document(metadata={'source': './file/fake.docx'}, page_content='可能面临相关法律诉讼或处罚。\\n\\n\\n\\n七、结论与建议\\n\\n综上所述'), Document(metadata={'source': './file/fake.docx'}, page_content='宏图科技发展有限公司目前处于财务困境之中，若无外部资金注入或业务转型成功'), Document(metadata={'source': './file/fake.docx'}, page_content='短期内难以扭转局势。对于不良资产收购方来说，在考虑收购宏图科技的相关资产前，建议进行深入的尽职调查'), Document(metadata={'source': './file/fake.docx'}, page_content='并制定详细的风险控制和资产处置方案。同时'), Document(metadata={'source': './file/fake.docx'}, page_content='在估值时应充分考虑到公司所面临的各种潜在风险和清收难度。\\n\\n\\n\\n报告撰写日期：2023年4月20日')]\n",
      "根据提供的文本信息，宏图科技发展有限公司的财务状况和经营状况存在严重问题，以下是对该公司的评价与分析：\n",
      "\n",
      "### 1. **财务状况堪忧**\n",
      "   - **资产与负债失衡**：公司资产总额为人民币1.2亿元，而负债总额高达1.8亿元，资不抵债，财务状况极为脆弱。\n",
      "   - **现金流紧张**：现金及现金等价物余额仅为500万元，难以支撑日常运营，表明公司流动性严重不足。\n",
      "   - **亏损严重**：2023年第一季度净利润为亏损800万元，去年同期为盈利200万元，盈利能力大幅下降。\n",
      "   - **应收账款与存货问题**：应收账款高达600万元，且坏账准备不足；存货积压严重，库存商品价值400万元，大部分产品滞销，资产变现能力差。\n",
      "\n",
      "### 2. **经营风险高**\n",
      "   - **主营业务收入下降**：由于市场竞争加剧、技术更新换代速度快以及管理层决策失误，公司主营业务收入持续下降。\n",
      "   - **产品缺乏竞争力**：产品同质化严重，缺乏核心竞争力，新产品开发进度缓慢，未能及时抓住市场需求变化。\n",
      "   - **客户流失严重**：原有客户流失严重，市场份额大幅缩水，进一步加剧了经营困境。\n",
      "\n",
      "### 3. **债务问题严峻**\n",
      "   - **银行贷款逾期**：公司向多家银行贷款总额达1亿元，部分贷款已逾期未还，面临诉讼或资产被查封的风险。\n",
      "   - **供应商欠款与员工工资拖欠**：拖欠供应商货款300万元，员工工资及社保费用200万元，现金流断裂导致债务问题进一步恶化。\n",
      "   - **其他应付款项**：包括税费、租赁费用等累计约100万元，债务压力巨大。\n",
      "\n",
      "### 4. **资产质量较差**\n",
      "   - **固定资产折旧严重**：公司办公用房和设备原值800万元，累计折旧400万元，实际价值大幅缩水。\n",
      "   - **无形资产价值有限**：软件著作权、专利权等无形资产原值300万元，但在当前市场环境下，变现能力存疑。\n",
      "   - **存货与应收账款难以回收**：存货滞销，应收账款回收难度大，资产流动性极低。\n",
      "\n",
      "### 5. **潜在风险高**\n",
      "   - **破产清算风险**：若短期内无法筹措足够资金偿还债务，公司可能面临破产清算。\n",
      "   - **法律风险**：因未能按时支付债务和相关费用，可能面临法律诉讼或处罚。\n",
      "   - **市场风险**：行业竞争加剧，市场需求不明朗，新兴企业崛起迅速，进一步压缩了公司的生存空间。\n",
      "\n",
      "### 6. **结论与建议**\n",
      "   - **短期内难以扭转局势**：公司财务状况和经营状况均处于严重困境，若无外部资金注入或业务转型成功，未来业绩可能继续恶化。\n",
      "   - **建议谨慎收购**：对于潜在的不良资产收购方，建议进行深入的尽职调查，充分评估公司面临的潜在风险和资产清收难度，并制定详细的风险控制和资产处置方案。\n",
      "\n",
      "### 总结：\n",
      "宏图科技发展有限公司目前处于严重的财务和经营困境中，资不抵债、现金流紧张、债务问题严峻、资产质量较差，且面临破产清算的高风险。公司缺乏核心竞争力和市场应变能力，短期内难以扭转局势。对于潜在投资者或收购方而言，需谨慎评估风险，避免因资产质量差和债务问题导致进一步损失。\n"
     ]
    }
   ],
   "source": [
    "chatDocx = ChatDocx()\n",
    "chatDocx.doc='./file/fake.docx'\n",
    "chatDocx.splitSentence()\n",
    "chatDocx.askToLLMWithPrompt(\"根据已有的信息评价分析这家公司\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#设置下logging查看生成查询\n",
    "import logging\n",
    "logging.basicConfig(level=logging.INFO)\n",
    "logging.getLogger(\"langchain.retrievers.multi_query\").setLevel(logging.DEBUG)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文档对话"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:langchain_text_splitters.base:Created a chunk of size 87, which is longer than the specified 50\n",
      "WARNING:langchain_text_splitters.base:Created a chunk of size 141, which is longer than the specified 50\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(metadata={'source': './file/fake.docx'}, page_content='一、公司基本信息\\n\\n名称：宏图科技发展有限公司\\n\\n注册地址：江苏省南京市雨花台区软件大道101号\\n\\n成立日期：2011年5月16日\\n\\n法定代表人：李强\\n\\n注册资本：人民币5000万元\\n\\n员工人数：约200人\\n\\n联系电话：025-88888888\\n\\n电子邮箱：info@hongtutech.cn\\n\\n\\n\\n二、财务状况概述\\n\\n截至2023年第一季度，宏图科技发展有限公司财务状况堪忧，具体情况如下：\\n\\n1. 资产总额：人民币1.2亿元，较上年同期下降30%。\\n\\n2. 负债总额：人民币1.8亿元，较上年同期上升50%，资不抵债。\\n\\n3. 营业收入：人民币3000万元，较上年同期下降60%。\\n\\n4. 净利润：亏损人民币800万元，去年同期为盈利人民币200万元。\\n\\n5. 现金流量：公司现金流量紧张，现金及现金等价物余额为人民币500万元，难以支撑日常运营。\\n\\n6. 存货：存货积压严重，库存商品价值约为人民币400万元，大部分产品滞销。\\n\\n7. 应收账款：应收账款高达人民币600万元，回收难度大，坏账准备不足。\\n\\n\\n\\n三、主营业务及市场状况\\n\\n宏图科技发展有限公司主要从事计算机软件的研发与销售。近年来，由于市场竞争加剧、技术更新换代速度快和管理层决策失误等原因，公司主营业务收入持续下降。目前，公司面临的主要问题有：\\n\\n1. 产品同质化严重，缺乏核心竞争力。\\n\\n2. 新产品开发进度缓慢，未能及时抓住市场需求变化。\\n\\n3. 市场营销策略不当，导致市场份额大幅缩水。\\n\\n4. 行业内新兴企业崛起迅速，原有客户流失严重。\\n\\n\\n\\n四、债权债务情况\\n\\n宏图科技发展有限公司目前面临的债务问题严峻，具体情况如下：\\n\\n1. 银行贷款：公司向多家银行贷款总额达人民币1亿元，部分贷款已逾期未还。\\n\\n2. 供应商欠款：因现金流紧张，公司拖欠供应商货款达人民币300万元。\\n\\n3. 员工工资及社保：由于资金链断裂，公司拖欠员工工资及社保费用共计人民币200万元。\\n\\n4. 其他应付款项：包括税费、租赁费用等其他应付款项累计约人民币100万元。\\n\\n\\n\\n五、资产清单\\n\\n宏图科技发展有限公司目前拥有的主要资产包括：\\n\\n1. 固定资产：公司办公用房和设备原值合计人民币800万元，累计折旧约400万元。\\n\\n2. 无形资产：包括软件著作权、专利权等无形资产原值合计人民币300万元。\\n\\n3. 存货资产：存货包括已完成软件产品和半成品，价值约为人民币400万元。\\n\\n4. 应收账款：主要包括对外销售软件的应收账款合计人民币600万元。\\n\\n\\n\\n六、潜在风险及预警\\n\\n1. 经营风险：由于连续亏损，公司可能面临破产清算的风险。\\n\\n2. 债务风险：负债累累，若短期内无法筹措足够资金偿还债务，可能面临诉讼或资产被查封的风险。\\n\\n3. 市场风险：行业竞争加剧和市场需求不明朗，可能导致公司未来业绩继续恶化。\\n\\n4. 法律风险：因未能按时支付债务和相关费用，可能面临相关法律诉讼或处罚。\\n\\n\\n\\n七、结论与建议\\n\\n综上所述，宏图科技发展有限公司目前处于财务困境之中，若无外部资金注入或业务转型成功，短期内难以扭转局势。对于不良资产收购方来说，在考虑收购宏图科技的相关资产前，建议进行深入的尽职调查，并制定详细的风险控制和资产处置方案。同时，在估值时应充分考虑到公司所面临的各种潜在风险和清收难度。\\n\\n\\n\\n报告撰写日期：2023年4月20日')]\n",
      "[Document(metadata={'source': './file/fake.docx'}, page_content='一、公司基本信息\\n\\n名称：宏图科技发展有限公司\\n\\n注册地址：江苏省南京市雨花台区软件大道101号'), Document(metadata={'source': './file/fake.docx'}, page_content='成立日期：2011年5月16日\\n\\n法定代表人：李强\\n\\n注册资本：人民币5000万元'), Document(metadata={'source': './file/fake.docx'}, page_content='员工人数：约200人\\n\\n联系电话：025-88888888'), Document(metadata={'source': './file/fake.docx'}, page_content='电子邮箱：info@hongtutech.cn\\n\\n二、财务状况概述'), Document(metadata={'source': './file/fake.docx'}, page_content='截至2023年第一季度，宏图科技发展有限公司财务状况堪忧，具体情况如下：'), Document(metadata={'source': './file/fake.docx'}, page_content='1. 资产总额：人民币1.2亿元，较上年同期下降30%。'), Document(metadata={'source': './file/fake.docx'}, page_content='2. 负债总额：人民币1.8亿元，较上年同期上升50%，资不抵债。'), Document(metadata={'source': './file/fake.docx'}, page_content='3. 营业收入：人民币3000万元，较上年同期下降60%。'), Document(metadata={'source': './file/fake.docx'}, page_content='4. 净利润：亏损人民币800万元，去年同期为盈利人民币200万元。'), Document(metadata={'source': './file/fake.docx'}, page_content='5. 现金流量：公司现金流量紧张，现金及现金等价物余额为人民币500万元，难以支撑日常运营。'), Document(metadata={'source': './file/fake.docx'}, page_content='6. 存货：存货积压严重，库存商品价值约为人民币400万元，大部分产品滞销。'), Document(metadata={'source': './file/fake.docx'}, page_content='7. 应收账款：应收账款高达人民币600万元，回收难度大，坏账准备不足。\\n\\n三、主营业务及市场状况'), Document(metadata={'source': './file/fake.docx'}, page_content='宏图科技发展有限公司主要从事计算机软件的研发与销售。近年来，由于市场竞争加剧、技术更新换代速度快和管理层决策失误等原因，公司主营业务收入持续下降。目前，公司面临的主要问题有：'), Document(metadata={'source': './file/fake.docx'}, page_content='1. 产品同质化严重，缺乏核心竞争力。\\n\\n2. 新产品开发进度缓慢，未能及时抓住市场需求变化。'), Document(metadata={'source': './file/fake.docx'}, page_content='3. 市场营销策略不当，导致市场份额大幅缩水。\\n\\n4. 行业内新兴企业崛起迅速，原有客户流失严重。'), Document(metadata={'source': './file/fake.docx'}, page_content='四、债权债务情况\\n\\n宏图科技发展有限公司目前面临的债务问题严峻，具体情况如下：'), Document(metadata={'source': './file/fake.docx'}, page_content='1. 银行贷款：公司向多家银行贷款总额达人民币1亿元，部分贷款已逾期未还。'), Document(metadata={'source': './file/fake.docx'}, page_content='2. 供应商欠款：因现金流紧张，公司拖欠供应商货款达人民币300万元。'), Document(metadata={'source': './file/fake.docx'}, page_content='3. 员工工资及社保：由于资金链断裂，公司拖欠员工工资及社保费用共计人民币200万元。'), Document(metadata={'source': './file/fake.docx'}, page_content='4. 其他应付款项：包括税费、租赁费用等其他应付款项累计约人民币100万元。\\n\\n五、资产清单'), Document(metadata={'source': './file/fake.docx'}, page_content='宏图科技发展有限公司目前拥有的主要资产包括：'), Document(metadata={'source': './file/fake.docx'}, page_content='1. 固定资产：公司办公用房和设备原值合计人民币800万元，累计折旧约400万元。'), Document(metadata={'source': './file/fake.docx'}, page_content='2. 无形资产：包括软件著作权、专利权等无形资产原值合计人民币300万元。'), Document(metadata={'source': './file/fake.docx'}, page_content='3. 存货资产：存货包括已完成软件产品和半成品，价值约为人民币400万元。'), Document(metadata={'source': './file/fake.docx'}, page_content='4. 应收账款：主要包括对外销售软件的应收账款合计人民币600万元。\\n\\n六、潜在风险及预警'), Document(metadata={'source': './file/fake.docx'}, page_content='1. 经营风险：由于连续亏损，公司可能面临破产清算的风险。'), Document(metadata={'source': './file/fake.docx'}, page_content='2. 债务风险：负债累累，若短期内无法筹措足够资金偿还债务，可能面临诉讼或资产被查封的风险。'), Document(metadata={'source': './file/fake.docx'}, page_content='3. 市场风险：行业竞争加剧和市场需求不明朗，可能导致公司未来业绩继续恶化。'), Document(metadata={'source': './file/fake.docx'}, page_content='4. 法律风险：因未能按时支付债务和相关费用，可能面临相关法律诉讼或处罚。\\n\\n七、结论与建议'), Document(metadata={'source': './file/fake.docx'}, page_content='综上所述，宏图科技发展有限公司目前处于财务困境之中，若无外部资金注入或业务转型成功，短期内难以扭转局势。对于不良资产收购方来说，在考虑收购宏图科技的相关资产前，建议进行深入的尽职调查，并制定详细的风险控制和资产处置方案。同时，在估值时应充分考虑到公司所面临的各种潜在风险和清收难度。'), Document(metadata={'source': './file/fake.docx'}, page_content='报告撰写日期：2023年4月20日')]\n",
      "\"上下文内容:宏图科技发展有限公司目前拥有的主要资产包括：1. 银行贷款：公司向多家银行贷款总额达人民币1亿元，部分贷款已逾期未还。1. 固定资产：公司办公用房和设备原值合计人民币800万元，累计折旧约400万元。\"\n",
      "\"问题：这家公司法定代表人是谁\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "AIMessage(content='根据提供的上下文内容，没有提到宏图科技发展有限公司的法定代表人信息。如果您需要了解法定代表人的具体信息，建议查阅公司的工商登记资料或联系公司相关部门获取准确信息。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 39, 'prompt_tokens': 112, 'total_tokens': 151, 'completion_tokens_details': None, 'prompt_tokens_details': None, 'prompt_cache_hit_tokens': 64, 'prompt_cache_miss_tokens': 48}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_3a5770e1b4', 'finish_reason': 'stop', 'logprobs': None}, id='run-1a83c879-67b8-49bd-aa36-e1882f4d6d8c-0', usage_metadata={'input_tokens': 112, 'output_tokens': 39, 'total_tokens': 151, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.document_loaders import UnstructuredExcelLoader,PyPDFLoader,Docx2txtLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.embeddings import CacheBackedEmbeddings\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "\n",
    "\n",
    "class ChatToDoc():\n",
    "    def __init__(self):\n",
    "        self.doc = None\n",
    "        self.splitText = [] #分割后的文本\n",
    "        self.template = [\n",
    "            (\"system\",\"你是一个处理文档的秘书,你从不说自己是一个大模型或者AI助手,你会根据下面提供的上下文内容来继续回答问题.\\n 上下文内容\\n {context} \\n\"),\n",
    "            (\"human\",\"你好！\"),\n",
    "            (\"ai\",\"你好\"),\n",
    "            (\"human\",\"{question}\"),\n",
    "        ]\n",
    "        self.prompt = ChatPromptTemplate.from_messages(self.template)\n",
    "        self.llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "    \n",
    "\n",
    "    # 读取文档内容\n",
    "    def getFile(self):\n",
    "        doc = self.doc\n",
    "        loaders = {\n",
    "            \"docx\":Docx2txtLoader,\n",
    "            \"pdf\":PyPDFLoader,\n",
    "            \"xlsx\":UnstructuredExcelLoader\n",
    "        }\n",
    "        #获取当前文件的后缀\n",
    "        full_extension = doc.split(\".\")[-1]\n",
    "        loader_class = loaders.get(full_extension)\n",
    "        if loader_class:\n",
    "            try:\n",
    "                loader = loader_class(doc)\n",
    "                return loader.load()\n",
    "            except Exception as e:\n",
    "                print(f'Error: {e}')\n",
    "                return None\n",
    "        else:\n",
    "            print(f'Error: Unsupported file format.')\n",
    "            return None\n",
    "    \n",
    "\n",
    "    #处理文档\n",
    "    def splitSentence(self):\n",
    "        raw_doc = self.getFile()\n",
    "        print(raw_doc)\n",
    "        # 文档分割器\n",
    "        # text_split = CharacterTextSplitter(chunk_size=50,chunk_overlap=0,separator=\"，\")\n",
    "        text_split = CharacterTextSplitter(chunk_size=50,chunk_overlap=0)\n",
    "        split_text = text_split.split_documents(raw_doc)\n",
    "        print(split_text)\n",
    "        self.split_text = split_text\n",
    "\n",
    "\n",
    "    # 构建并返回向量数据库\n",
    "    def embeddingVectorDB(self):\n",
    "        # 初始化向量模型\n",
    "        u_embeddings = HuggingFaceEmbeddings(\n",
    "            #  model_name='maidalun1020/bce-embedding-base_v1',\n",
    "             model_name='sentence-transformers/all-MiniLM-L6-v2',\n",
    "             model_kwargs = {'device': 'cpu'},\n",
    "             encode_kwargs= {'batch_size': 64, 'normalize_embeddings': True}\n",
    "            )\n",
    "        fs = LocalFileStore(\"./cache/\")\n",
    "\n",
    "        # 创建并使用向量缓存\n",
    "        cached_embeddings = CacheBackedEmbeddings.from_bytes_store(\n",
    "            u_embeddings,\n",
    "            fs,\n",
    "            namespace=u_embeddings.model_name,) \n",
    "        \n",
    "        db = FAISS.from_documents(self.split_text,cached_embeddings) \n",
    "\n",
    "        return db\n",
    "    \n",
    "\n",
    "    # 提问并获取相关的文本块------仅是从向量库中找出相关的语句\n",
    "    def askQuestion(self, question):\n",
    "        db = self.embeddingVectorDB()\n",
    "\n",
    "        retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "        doc = retriever.get_relevant_documents(question)\n",
    "        return doc\n",
    "    \n",
    "    #用自然语言和文档聊天\n",
    "    def chatWithDoc(self,question):\n",
    "        _content = \"\"\n",
    "        context = self.askQuestion(question)\n",
    "        for i in context:\n",
    "            _content += i.page_content\n",
    "        \n",
    "        print(f'\"上下文内容:{_content}\"' )\n",
    "        print(f'\"问题：{question}\"')\n",
    "        message = self.prompt.format(context=_content, question=question)\n",
    "        return self.llm.invoke(message)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "chatToDock = ChatToDoc()\n",
    "chatToDock.doc='./file/fake.docx'\n",
    "chatToDock.splitSentence()\n",
    "chatToDock.chatWithDoc(\"这家公司法定代表人是谁\")\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import UnstructuredExcelLoader,PyPDFLoader,Docx2txtLoader\n",
    "from langchain.embeddings import CacheBackedEmbeddings\n",
    "from langchain.storage import  LocalFileStore\n",
    "from langchain.document_loaders import TextLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "from langchain_community.vectorstores import FAISS\n",
    "\n",
    "\n",
    "from langchain_openai import ChatOpenAI\n",
    "# 多重向量检索\n",
    "from langchain.retrievers.multi_query import MultiQueryRetriever\n",
    "# 上下文压缩，降低冗余信息\n",
    "from langchain.retrievers import ContextualCompressionRetriever\n",
    "from langchain.retrievers.document_compressors import LLMChainExtractor\n",
    "# 调用llm模型，对话提高精准度\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain,StuffDocumentsChain\n",
    "from langchain.document_transformers import LongContextReorder\n",
    "\n",
    "\n",
    "class ChatDocx():\n",
    "    def __init__(self):\n",
    "        self.doc=None\n",
    "        self.split_text = []\n",
    "        self.llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "    \n",
    "    #处理文档\n",
    "    def splitSentence(self):\n",
    "        raw_doc = self.getFile()\n",
    "        print(raw_doc)\n",
    "        # 文档分割器\n",
    "        text_split = CharacterTextSplitter(chunk_size=50,chunk_overlap=0,separator=\"，\")\n",
    "        split_text = text_split.split_documents(raw_doc)\n",
    "        print(split_text)\n",
    "        self.split_text = split_text\n",
    "        # %timeit -r 1 -n 1 db = FAISS.from_documents(documents,cached_embeddings)\n",
    "\n",
    "    # 构建并返回向量数据库\n",
    "    def embeddingVectorDB(self):\n",
    "        # 初始化向量模型\n",
    "        u_embeddings = HuggingFaceEmbeddings(\n",
    "            #  model_name='maidalun1020/bce-embedding-base_v1',\n",
    "             model_name='sentence-transformers/all-MiniLM-L6-v2',\n",
    "             model_kwargs = {'device': 'cpu'},\n",
    "             encode_kwargs= {'batch_size': 64, 'normalize_embeddings': True}\n",
    "            )\n",
    "        fs = LocalFileStore(\"./cache/\")\n",
    "\n",
    "        # 创建并使用向量缓存\n",
    "        cached_embeddings = CacheBackedEmbeddings.from_bytes_store(\n",
    "            u_embeddings,\n",
    "            fs,\n",
    "            namespace=u_embeddings.model_name,) \n",
    "        \n",
    "        db = FAISS.from_documents(self.split_text,cached_embeddings) \n",
    "\n",
    "        return db\n",
    "\n",
    "\n",
    "    #获取文件内容\n",
    "    def getFile(self):\n",
    "        doc = self.doc\n",
    "        loaders = {\n",
    "            \"docx\":Docx2txtLoader,\n",
    "            \"pdf\":PyPDFLoader,\n",
    "            \"xlsx\":UnstructuredExcelLoader\n",
    "        }\n",
    "        #获取当前文件的后缀\n",
    "        full_extension = doc.split(\".\")[-1]\n",
    "        loader_class = loaders.get(full_extension)\n",
    "        if loader_class:\n",
    "            try:\n",
    "                loader = loader_class(doc)\n",
    "                return loader.load()\n",
    "            except Exception as e:\n",
    "                print(f'Error: {e}')\n",
    "                return None\n",
    "        else:\n",
    "            print(f'Error: Unsupported file format.')\n",
    "            return None\n",
    "\n",
    "\n",
    "    # 提问并获取相关的文本块------仅是从向量库中找出相关的语句\n",
    "    def askQuestion(self, question):\n",
    "        db = self.embeddingVectorDB()\n",
    "\n",
    "        retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "        doc = retriever.get_relevant_documents(question)\n",
    "        return doc\n",
    "\n",
    "    # 多重向量检索\n",
    "    def askQuestionMulti(self, question):\n",
    "        db = self.embeddingVectorDB()\n",
    "\n",
    "        retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "        \n",
    "        retriever_from_llm = MultiQueryRetriever.from_llm(retriever=retriever,llm=self.llm)\n",
    "        doc = retriever_from_llm.get_relevant_documents(question)\n",
    "        return doc\n",
    "\n",
    "    # 模型寻找相关的文本块\n",
    "    def askAndFindFiles(self, question):\n",
    "        db = self.embeddingVectorDB()\n",
    "        retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "        compress = LLMChainExtractor.from_llm(llm = self.llm)\n",
    "        compressor_retriever = ContextualCompressionRetriever(base_retriever=retriever, base_compressor = compress)\n",
    "        return compressor_retriever.get_relevant_documents(query=question)\n",
    "\n",
    "\n",
    "    # 调用llm模型，传入文本prompt提供对话上下文，并进行对话\n",
    "    def askToLLMWithPrompt(self,question):\n",
    "        reordering = LongContextReorder()\n",
    "        reo_docs = reordering.transform_documents(self.split_text)\n",
    "        print(self.split_text)\n",
    "        # 构建文本prompt\n",
    "        doc_pormpt = PromptTemplate(input_variables=['page_content'], template='{page_content}')\n",
    "        # 其他代码保持不变\n",
    "        stuff_prompt_override = \"\"\"Given this text extracts:\n",
    "        ----------------------------------------\n",
    "        {context}\n",
    "        ----------------------------------------\n",
    "        Please answer the following questions:\n",
    "        {query}\n",
    "        \"\"\"\n",
    "\n",
    "        # 定义llm_prompt\n",
    "        prompt = PromptTemplate(template=stuff_prompt_override, input_variables=['context', 'query'])\n",
    "\n",
    "        # 构建chain\n",
    "        llm_chain = LLMChain(llm=self.llm, prompt=prompt)\n",
    "\n",
    "        # 构建stuff_documents_chain\n",
    "        workChain = StuffDocumentsChain(llm_chain=llm_chain, document_prompt=doc_pormpt, document_variable_name=\"context\")\n",
    "\n",
    "        # 构建问题\n",
    "        result = workChain.run(input_documents=reo_docs, query=question)\n",
    "        print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"上下文内容:宏图科技发展有限公司目前拥有的主要资产包括：1. 固定资产：公司办公用房和设备原值合计人民币800万元，累计折旧约400万元。1. 银行贷款：公司向多家银行贷款总额达人民币1亿元，部分贷款已逾期未还。\"\n",
      "\"问题：这家公司的固定资产\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "AIMessage(content='宏图科技发展有限公司的固定资产主要包括公司办公用房和设备。根据提供的信息，这些固定资产的原值合计为人民币800万元，累计折旧约为400万元。这意味着固定资产的账面净值为400万元（原值800万元减去累计折旧400万元）。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 52, 'prompt_tokens': 111, 'total_tokens': 163, 'completion_tokens_details': None, 'prompt_tokens_details': None, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 111}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_3a5770e1b4', 'finish_reason': 'stop', 'logprobs': None}, id='run-d94d1af5-1fa6-428c-80f9-2eac938a38ed-0', usage_metadata={'input_tokens': 111, 'output_tokens': 52, 'total_tokens': 163, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chatToDock.chatWithDoc(\"这家公司的固定资产\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"上下文内容:\"\n",
      "\"问题：你是谁？\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "AIMessage(content='我是您的文档处理秘书，随时准备帮助您处理和管理文档相关的事务。请问有什么我可以为您效劳的吗？', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 24, 'prompt_tokens': 54, 'total_tokens': 78, 'completion_tokens_details': None, 'prompt_tokens_details': None, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 54}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_3a5770e1b4', 'finish_reason': 'stop', 'logprobs': None}, id='run-1b79fa57-c855-4f45-a059-b3a4c94990a2-0', usage_metadata={'input_tokens': 54, 'output_tokens': 24, 'total_tokens': 78, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chatToDock.chatWithDoc(\"你是谁？\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# chain"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "四种通用链"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LLMChain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.prompts import PromptTemplate\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "    \n",
    "prompt_sim = '帮我给{product}想三个可以注册的域名?'\n",
    "\n",
    "llm_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=PromptTemplate.from_template(prompt_sim),\n",
    "    verbose = True\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\4014822242.py:1: LangChainDeprecationWarning: The method `Chain.__call__` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
      "  llm_chain(\"深库\")\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3m帮我给深库想三个可以注册的域名?\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'product': '深库',\n",
       " 'text': '当然可以！以下是三个可能适合“深库”的域名建议：\\n\\n1. **shenku.com**  \\n   - 简洁易记，直接使用拼音，适合品牌推广。\\n\\n2. deepvault.cn  \\n   - 结合“深”的英文“deep”和“库”的英文“vault”，适合国际化或科技类企业。\\n\\n3. skdata.net  \\n   - 使用“深库”拼音首字母“sk”加上“data”（数据），适合数据相关业务。\\n\\n你可以根据具体业务需求选择最适合的域名，并检查是否可注册。如果需要更多建议，可以告诉我更多细节！'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm_chain(\"深库\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 顺序链 SimpleSequentialChain & SequentialChain\n",
    "\n",
    "只支持固定的链路"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.chains import SimpleSequentialChain\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "    \n",
    "prompt_sim = '帮我给{product}的公司起一个响亮容易记忆的名字??'\n",
    "\n",
    "llm_chain1 = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=ChatPromptTemplate.from_template(prompt_sim),\n",
    "    verbose = True\n",
    ")\n",
    "\n",
    "\n",
    "prompt_sim2 = '用5个词来描述一下这个公司名字:{company_name}'\n",
    "\n",
    "llm_chain2 = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=ChatPromptTemplate.from_template(prompt_sim2),\n",
    "    verbose = True\n",
    ")\n",
    "\n",
    "simple_chain = SimpleSequentialChain(chains=[llm_chain1,llm_chain2], verbose=True)\n",
    "# simple_chain = SimpleSequentialChain(chains=[llm_chain1], verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mHuman: 帮我给视觉分析产品的公司起一个响亮容易记忆的名字??\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\u001b[36;1m\u001b[1;3m## 视觉分析产品公司名字建议：\n",
      "\n",
      "**简洁易记型:**\n",
      "\n",
      "* **视界 (Shijie)** -  简洁有力，突出视觉和洞察力。\n",
      "* **慧眼 (Huiyan)** -  寓意公司产品能帮助用户洞察数据，发现价值。\n",
      "* **洞察 (Dongcha)** -  直接点明公司业务，简单易记。\n",
      "* **数视 (Shushi)** -  结合“数据”和“视觉”，科技感强。\n",
      "* **图灵 (Turing)** -  致敬计算机科学之父，体现技术实力。\n",
      "\n",
      "**科技感强型:**\n",
      "\n",
      "* **深眸科技 (Shenmou Tech)** -  “深眸”寓意深度洞察，科技感强。\n",
      "* **智图科技 (Zhitu Tech)** -  “智图”寓意智能图像分析，科技感强。\n",
      "* **云瞳科技 (Yuntong Tech)** -  “云瞳”寓意云端视觉分析，科技感强。\n",
      "* **视界云 (Shijie Cloud)** -  结合“视界”和“云”，科技感强。\n",
      "* **图灵视界 (Turing Vision)** -  结合“图灵”和“视界”，科技感强。\n",
      "\n",
      "**创意独特型:**\n",
      "\n",
      "* **鹰眼 (Eagle Eye)** -  比喻公司产品像鹰眼一样敏锐，洞察力强。\n",
      "* **千里眼 (Qianliyan)** -  比喻公司产品能帮助用户看得更远，洞察更深。\n",
      "* **数据之眼 (Data Eye)** -  直接点明公司业务，创意独特。\n",
      "* **视界魔方 (Vision Cube)** -  比喻公司产品像魔方一样，能从不同角度解读数据。\n",
      "* **图灵之眼 (Turing Eye)** -  结合“图灵”和“眼”，创意独特。\n",
      "\n",
      "**英文名建议:**\n",
      "\n",
      "* **InsightVision** -  结合“insight”和“vision”，简洁易记。\n",
      "* **DataEye** -  直接点明公司业务，简单易记。\n",
      "* **DeepSight** -  寓意深度洞察，科技感强。\n",
      "* **VisionAI** -  结合“vision”和“AI”，科技感强。\n",
      "* **TuringVision** -  结合“Turing”和“vision”，科技感强。\n",
      "\n",
      "**选择名字时，还需要考虑以下因素:**\n",
      "\n",
      "* 目标客户群体\n",
      "* 公司文化和发展方向\n",
      "* 域名和商标的可用性\n",
      "\n",
      "希望以上建议能帮到您！\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mHuman: 用5个词来描述一下这个公司名字:## 视觉分析产品公司名字建议：\n",
      "\n",
      "**简洁易记型:**\n",
      "\n",
      "* **视界 (Shijie)** -  简洁有力，突出视觉和洞察力。\n",
      "* **慧眼 (Huiyan)** -  寓意公司产品能帮助用户洞察数据，发现价值。\n",
      "* **洞察 (Dongcha)** -  直接点明公司业务，简单易记。\n",
      "* **数视 (Shushi)** -  结合“数据”和“视觉”，科技感强。\n",
      "* **图灵 (Turing)** -  致敬计算机科学之父，体现技术实力。\n",
      "\n",
      "**科技感强型:**\n",
      "\n",
      "* **深眸科技 (Shenmou Tech)** -  “深眸”寓意深度洞察，科技感强。\n",
      "* **智图科技 (Zhitu Tech)** -  “智图”寓意智能图像分析，科技感强。\n",
      "* **云瞳科技 (Yuntong Tech)** -  “云瞳”寓意云端视觉分析，科技感强。\n",
      "* **视界云 (Shijie Cloud)** -  结合“视界”和“云”，科技感强。\n",
      "* **图灵视界 (Turing Vision)** -  结合“图灵”和“视界”，科技感强。\n",
      "\n",
      "**创意独特型:**\n",
      "\n",
      "* **鹰眼 (Eagle Eye)** -  比喻公司产品像鹰眼一样敏锐，洞察力强。\n",
      "* **千里眼 (Qianliyan)** -  比喻公司产品能帮助用户看得更远，洞察更深。\n",
      "* **数据之眼 (Data Eye)** -  直接点明公司业务，创意独特。\n",
      "* **视界魔方 (Vision Cube)** -  比喻公司产品像魔方一样，能从不同角度解读数据。\n",
      "* **图灵之眼 (Turing Eye)** -  结合“图灵”和“眼”，创意独特。\n",
      "\n",
      "**英文名建议:**\n",
      "\n",
      "* **InsightVision** -  结合“insight”和“vision”，简洁易记。\n",
      "* **DataEye** -  直接点明公司业务，简单易记。\n",
      "* **DeepSight** -  寓意深度洞察，科技感强。\n",
      "* **VisionAI** -  结合“vision”和“AI”，科技感强。\n",
      "* **TuringVision** -  结合“Turing”和“vision”，科技感强。\n",
      "\n",
      "**选择名字时，还需要考虑以下因素:**\n",
      "\n",
      "* 目标客户群体\n",
      "* 公司文化和发展方向\n",
      "* 域名和商标的可用性\n",
      "\n",
      "希望以上建议能帮到您！\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\u001b[33;1m\u001b[1;3m## 视觉分析产品公司名字建议：\n",
      "\n",
      "**简洁易记型:**\n",
      "\n",
      "* **视界 (Shijie)** -  简洁有力，突出视觉和洞察力。\n",
      "* **慧眼 (Huiyan)** -  寓意公司产品能帮助用户洞察数据，发现价值。\n",
      "* **洞察 (Dongcha)** -  直接点明公司业务，简单易记。\n",
      "* **数视 (Shushi)** -  结合“数据”和“视觉”，科技感强。\n",
      "* **图灵 (Turing)** -  致敬计算机科学之父，体现技术实力。\n",
      "\n",
      "**科技感强型:**\n",
      "\n",
      "* **深眸科技 (Shenmou Tech)** -  “深眸”寓意深度洞察，科技感强。\n",
      "* **智图科技 (Zhitu Tech)** -  “智图”寓意智能图像分析，科技感强。\n",
      "* **云瞳科技 (Yuntong Tech)** -  “云瞳”寓意云端视觉分析，科技感强。\n",
      "* **视界云 (Shijie Cloud)** -  结合“视界”和“云”，科技感强。\n",
      "* **图灵视界 (Turing Vision)** -  结合“图灵”和“视界”，科技感强。\n",
      "\n",
      "**创意独特型:**\n",
      "\n",
      "* **鹰眼 (Eagle Eye)** -  比喻公司产品像鹰眼一样敏锐，洞察力强。\n",
      "* **千里眼 (Qianliyan)** -  比喻公司产品能帮助用户看得更远，洞察更深。\n",
      "* **数据之眼 (Data Eye)** -  直接点明公司业务，创意独特。\n",
      "* **视界魔方 (Vision Cube)** -  比喻公司产品像魔方一样，能从不同角度解读数据。\n",
      "* **图灵之眼 (Turing Eye)** -  结合“图灵”和“眼”，创意独特。\n",
      "\n",
      "**英文名建议:**\n",
      "\n",
      "* **InsightVision** -  结合“insight”和“vision”，简洁易记。\n",
      "* **DataEye** -  直接点明公司业务，简单易记。\n",
      "* **DeepSight** -  寓意深度洞察，科技感强。\n",
      "* **VisionAI** -  结合“vision”和“AI”，科技感强。\n",
      "* **TuringVision** -  结合“Turing”和“vision”，科技感强。\n",
      "\n",
      "**选择名字时，还需要考虑以下因素:**\n",
      "\n",
      "* 目标客户群体\n",
      "* 公司文化和发展方向\n",
      "* 域名和商标的可用性\n",
      "\n",
      "希望以上建议能帮到您！\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '视觉分析产品',\n",
       " 'output': '## 视觉分析产品公司名字建议：\\n\\n**简洁易记型:**\\n\\n* **视界 (Shijie)** -  简洁有力，突出视觉和洞察力。\\n* **慧眼 (Huiyan)** -  寓意公司产品能帮助用户洞察数据，发现价值。\\n* **洞察 (Dongcha)** -  直接点明公司业务，简单易记。\\n* **数视 (Shushi)** -  结合“数据”和“视觉”，科技感强。\\n* **图灵 (Turing)** -  致敬计算机科学之父，体现技术实力。\\n\\n**科技感强型:**\\n\\n* **深眸科技 (Shenmou Tech)** -  “深眸”寓意深度洞察，科技感强。\\n* **智图科技 (Zhitu Tech)** -  “智图”寓意智能图像分析，科技感强。\\n* **云瞳科技 (Yuntong Tech)** -  “云瞳”寓意云端视觉分析，科技感强。\\n* **视界云 (Shijie Cloud)** -  结合“视界”和“云”，科技感强。\\n* **图灵视界 (Turing Vision)** -  结合“图灵”和“视界”，科技感强。\\n\\n**创意独特型:**\\n\\n* **鹰眼 (Eagle Eye)** -  比喻公司产品像鹰眼一样敏锐，洞察力强。\\n* **千里眼 (Qianliyan)** -  比喻公司产品能帮助用户看得更远，洞察更深。\\n* **数据之眼 (Data Eye)** -  直接点明公司业务，创意独特。\\n* **视界魔方 (Vision Cube)** -  比喻公司产品像魔方一样，能从不同角度解读数据。\\n* **图灵之眼 (Turing Eye)** -  结合“图灵”和“眼”，创意独特。\\n\\n**英文名建议:**\\n\\n* **InsightVision** -  结合“insight”和“vision”，简洁易记。\\n* **DataEye** -  直接点明公司业务，简单易记。\\n* **DeepSight** -  寓意深度洞察，科技感强。\\n* **VisionAI** -  结合“vision”和“AI”，科技感强。\\n* **TuringVision** -  结合“Turing”和“vision”，科技感强。\\n\\n**选择名字时，还需要考虑以下因素:**\\n\\n* 目标客户群体\\n* 公司文化和发展方向\\n* 域名和商标的可用性\\n\\n希望以上建议能帮到您！'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simple_chain('视觉分析产品')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.chains import SequentialChain\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "#chain 1 任务：翻译成中文\n",
    "first_prompt = ChatPromptTemplate.from_template(\"把下面内容翻译成中文:\\n\\n{content}\")\n",
    "chain_one = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=first_prompt,\n",
    "    verbose=True,\n",
    "    output_key=\"Chinese_Rview\",\n",
    ")\n",
    "\n",
    "#chain 2 任务：对翻译后的中文进行总结摘要 input_key是上一个chain的output_key\n",
    "second_prompt = ChatPromptTemplate.from_template(\"用一句话总结下面内容:\\n\\n{Chinese_Rview}\")\n",
    "chain_two = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=second_prompt,\n",
    "    verbose=True,\n",
    "    output_key=\"Chinese_Summary\",\n",
    ")\n",
    "\n",
    "#chain 3 任务:智能识别语言 input_key是上一个chain的output_key\n",
    "third_prompt = ChatPromptTemplate.from_template(\"下面内容是什么语言:\\n\\n{Chinese_Summary}\")\n",
    "chain_three = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=third_prompt,\n",
    "    verbose=True,\n",
    "    output_key=\"Language\",\n",
    ")\n",
    "\n",
    "#chain 4 任务:针对摘要使用指定语言进行评论 input_key是上一个chain的output_key   \n",
    "fourth_prompt = ChatPromptTemplate.from_template(\"请使用指定的语言对以下内容进行回复:\\n\\n内容:{Chinese_Summary}\\n\\n语言:{Language}\")\n",
    "chain_four = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=fourth_prompt,\n",
    "    verbose=True,\n",
    "    output_key=\"Reply\",\n",
    ")\n",
    "\n",
    "#overall 任务：翻译成中文->对翻译后的中文进行总结摘要->智能识别语言->针对摘要使用指定语言进行评论\n",
    "overall_chain = SequentialChain(\n",
    "    chains=[chain_one, chain_two, chain_three, chain_four],\n",
    "    verbose=True,\n",
    "    input_variables=[\"content\"],\n",
    "    output_variables=[\"Chinese_Rview\", \"Chinese_Summary\", \"Language\", \"Reply\"],\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mHuman: 把下面内容翻译成中文:\n",
      "\n",
      "Recently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: michael.johnson@example.com).\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mHuman: 用一句话总结下面内容:\n",
      "\n",
      "最近，我们迎来了几位新团队成员，他们在各自的部门中做出了重要贡献。我想特别表彰简·史密斯（SSN: 049-45-5928）在客户服务方面的出色表现。简一直收到客户的高度评价。此外，请记住，我们员工福利计划的开放注册期即将到来。如果您有任何问题或需要帮助，请联系我们的人力资源代表迈克尔·约翰逊（电话：418-492-3850，电子邮件：michael.johnson@example.com）。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mHuman: 下面内容是什么语言:\n",
      "\n",
      "我们迎来了几位新团队成员并特别表彰简·史密斯在客户服务方面的出色表现，同时提醒员工福利计划的开放注册期即将到来，有问题可联系人力资源代表迈克尔·约翰逊。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mHuman: 请使用指定的语言对以下内容进行回复:\n",
      "\n",
      "内容:我们迎来了几位新团队成员并特别表彰简·史密斯在客户服务方面的出色表现，同时提醒员工福利计划的开放注册期即将到来，有问题可联系人力资源代表迈克尔·约翰逊。\n",
      "\n",
      "语言:这段内容使用的是**中文**。这是一段公司内部通知或公告，内容涉及新团队成员的加入、对员工简·史密斯的表彰、员工福利计划的开放注册提醒，以及联系人力资源代表迈克尔·约翰逊的方式。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'content': 'Recently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: michael.johnson@example.com).',\n",
       " 'Chinese_Rview': '最近，我们迎来了几位新团队成员，他们在各自的部门中做出了重要贡献。我想特别表彰简·史密斯（SSN: 049-45-5928）在客户服务方面的出色表现。简一直收到客户的高度评价。此外，请记住，我们员工福利计划的开放注册期即将到来。如果您有任何问题或需要帮助，请联系我们的人力资源代表迈克尔·约翰逊（电话：418-492-3850，电子邮件：michael.johnson@example.com）。',\n",
       " 'Chinese_Summary': '我们迎来了几位新团队成员并特别表彰简·史密斯在客户服务方面的出色表现，同时提醒员工福利计划的开放注册期即将到来，有问题可联系人力资源代表迈克尔·约翰逊。',\n",
       " 'Language': '这段内容使用的是**中文**。这是一段公司内部通知或公告，内容涉及新团队成员的加入、对员工简·史密斯的表彰、员工福利计划的开放注册提醒，以及联系人力资源代表迈克尔·约翰逊的方式。',\n",
       " 'Reply': '我们迎来了几位新团队成员，并特别表彰简·史密斯在客户服务方面的出色表现。同时，提醒大家员工福利计划的开放注册期即将到来，如有任何问题，请联系人力资源代表迈克尔·约翰逊。'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取文件\n",
    "content = \"Recently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: michael.johnson@example.com).\"\n",
    "overall_chain(content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RouterChain "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "#物理链\n",
    "physics_template = \"\"\"您是一位非常聪明的物理教授.\\n\n",
    "您擅长以简洁易懂的方式回答物理问题.\\n\n",
    "当您不知道问题答案的时候，您会坦率承认不知道.\\n\n",
    "下面是一个问题:\n",
    "{input}\"\"\"\n",
    "physics_prompt = PromptTemplate.from_template(physics_template)\n",
    "\n",
    "#数学链\n",
    "math_template = \"\"\"您是一位非常优秀的数学教授.\\n\n",
    "您擅长回答数学问题.\\n\n",
    "您之所以如此优秀，是因为您能够将困难问题分解成组成的部分，回答这些部分，然后将它们组合起来，回答更广泛的问题.\\n\n",
    "下面是一个问题:\n",
    "{input}\"\"\"\n",
    "math_prompt = PromptTemplate.from_template(math_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\2361921503.py:38: LangChainDeprecationWarning: The class `ConversationChain` was deprecated in LangChain 0.2.7 and will be removed in 1.0. Use :meth:`~RunnableWithMessageHistory: https://python.langchain.com/v0.2/api_reference/core/runnables/langchain_core.runnables.history.RunnableWithMessageHistory.html` instead.\n",
      "  default_chain = ConversationChain(llm = llm, output_key = \"text\" )\n",
      "c:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\pydantic\\main.py:212: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
      "  validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)\n"
     ]
    }
   ],
   "source": [
    "from langchain.chains import ConversationChain\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.llms import OpenAI\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "prompt_infos = [\n",
    "    {\n",
    "        \"name\":\"physics\",\n",
    "        \"description\":\"擅长回答物理问题\",\n",
    "        \"prompt_template\":physics_template,\n",
    "    },\n",
    "    {\n",
    "        \"name\":\"math\",\n",
    "        \"description\":\"擅长回答数学问题\",\n",
    "        \"prompt_template\":math_template,\n",
    "    },\n",
    "]\n",
    "\n",
    "destination_chains = {}\n",
    "\n",
    "for p_info in prompt_infos:\n",
    "    name = p_info[\"name\"]\n",
    "    description = p_info[\"description\"]\n",
    "    prompt_template = p_info[\"prompt_template\"]\n",
    "\n",
    "    prompt = PromptTemplate(template=prompt_template, input_variables=[\"input\"])\n",
    "    chain = LLMChain(llm=llm, prompt=prompt)\n",
    "    destination_chains[name] = chain\n",
    "\n",
    "    destination_chains[name] = chain\n",
    "\n",
    "default_chain = ConversationChain(llm = llm, output_key = \"text\" )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\3978228884.py:16: LangChainDeprecationWarning: Please see migration guide here for recommended implementation: https://python.langchain.com/docs/versions/migrating_chains/multi_prompt_chain/\n",
      "  chain = MultiPromptChain(\n"
     ]
    }
   ],
   "source": [
    "from langchain.chains.router.llm_router import LLMRouterChain,RouterOutputParser\n",
    "from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE\n",
    "from langchain.chains.router import MultiPromptChain\n",
    "\n",
    "descriptions = [f\"{p['name']}:{description}\" for p in prompt_infos]\n",
    "\n",
    "destinations_str = \"\\n\".join(descriptions)\n",
    "router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=destinations_str)\n",
    "\n",
    "# 由router_chain将问题进行解析，适配到descriptions字典，匹配到destination_chains中具体的chain\n",
    "route_prompt = PromptTemplate(template=router_template, input_variables=[\"input\"],output_parser=RouterOutputParser())\n",
    "router_chain = LLMRouterChain.from_llm(\n",
    "    llm,\n",
    "    route_prompt\n",
    ")\n",
    "\n",
    "chain = MultiPromptChain(\n",
    "    router_chain=router_chain,\n",
    "    destination_chains=destination_chains,\n",
    "    default_chain=default_chain,\n",
    "    verbose=True\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n",
      "math: {'input': '解释一下1+1'}\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'### 1. 理解基本概念\\n\\n首先，我需要明确“1”和“+”分别代表什么。\\n\\n- **数字1**：在数学中，1是最小的自然数，表示一个单一的单位或个体。它是计数和计算的基础。\\n  \\n- **加号（+）**：加号是数学中的基本运算符之一，表示将两个或多个数值相加，即求它们的总和。\\n\\n### 2. 加法的定义\\n\\n加法是一种基本的算术运算，用于计算两个或多个数的总和。具体来说，加法具有以下性质：\\n\\n- **交换律**：a + b = b + a。这意味着加数的顺序不影响结果。\\n  \\n- **结合律**：(a + b) + c = a + (b + c)。这意味着加数的分组方式不影响结果。\\n  \\n- **单位元**：任何数加上0都等于其本身，即a + 0 = a。\\n\\n### 3. 1 + 1的具体计算\\n\\n现在，我将具体计算1 + 1。\\n\\n- **步骤1**：确定加数。这里，两个加数都是1。\\n  \\n- **步骤2**：应用加法的定义，将两个1相加。\\n  \\n- **步骤3**：根据加法的性质，1 + 1 = 2。\\n\\n### 4. 验证结果\\n\\n为了确保我的计算正确，我可以通过以下方法验证：\\n\\n- **实物计数**：想象我有1个苹果，再得到1个苹果，总共有2个苹果。\\n  \\n- **数轴表示**：在数轴上，从1开始，向右移动1个单位，到达2的位置。\\n  \\n- **数学公理**：根据皮亚诺公理，1的后继数是2，因此1 + 1 = 2。\\n\\n### 5. 结论\\n\\n通过上述步骤和验证，我确认1 + 1确实等于2。这不仅是一个基本的数学事实，也是更复杂数学概念的基础。\\n\\n### 6. 进一步思考\\n\\n虽然1 + 1 = 2看似简单，但它在数学中扮演着重要角色。例如：\\n\\n- **二进制系统**：在计算机科学中，1 + 1 = 10（二进制），这展示了不同数制下的加法规则。\\n  \\n- **代数基础**：理解1 + 1 = 2有助于构建更复杂的代数表达式和方程。\\n  \\n- **逻辑推理**：加法运算在逻辑和证明中也有广泛应用，如归纳法的基础步骤。\\n\\n### 7. 总结\\n\\n通过分解问题、理解基本概念、应用定义和验证结果，我成功地解释了1 + 1等于2。这一过程不仅加深了我对加法的理解，也展示了数学思维的严谨性和逻辑性。\\n\\n---\\n\\n**最终答案**：1 + 1 = 2。'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.run(\"解释一下1+1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tramsformation  对话文档\n",
    "\n",
    "文档转换链"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件存在\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# 检查文件是否存在\n",
    "if os.path.exists(\"letter.txt\"):\n",
    "    print(\"文件存在\")\n",
    "else:\n",
    "    print(\"文件不存在，请检查路径\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 读取文档\n",
    "\n",
    "with open(\"letter.txt\") as f:\n",
    "    letters_text_trans = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import  (LLMChain, SimpleSequentialChain, TransformChain)\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "def transfrom_func(inputs:dict) -> dict:\n",
    "    text = inputs[\"text\"]\n",
    "    shortened_text = \"\\n\\n\".join(text.split(\"\\n\\n\")[:3])\n",
    "    return {\"output_text\":shortened_text}\n",
    "\n",
    "# 文档转换链\n",
    "transform_chain = TransformChain(\n",
    "    input_variables=[\"text\"],\n",
    "    output_variables=[\"output_text\"],\n",
    "    transform=transfrom_func,\n",
    ")\n",
    "\n",
    "template = \"\"\"对下面的文字进行总结:\n",
    "{output_text}\n",
    "\n",
    "总结:\"\"\"\n",
    "\n",
    "\n",
    "prompt = PromptTemplate(template=template, input_variables=[\"output_text\"])\n",
    "\n",
    "llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
    "\n",
    "# 顺序链串联\n",
    "simple_chain = SimpleSequentialChain(chains=[transform_chain, llm_chain], verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Generated with ChatGPT]\n",
      "\n",
      "Confidential Document - For Internal Use Only\n",
      "\n",
      "Date: July 1, 2023\n",
      "\n",
      "Subject: Updates and Discussions on Various Topics\n",
      "\n",
      "Dear Team,\n",
      "\n",
      "I hope this email finds you well. In this document, I would like to provide you with some important updates and discuss various topics that require our attention. Please treat the information contained herein as highly confidential.\n",
      "\n",
      "Security and Privacy Measures\n",
      "As part of our ongoing commitment to ensure the security and privacy of our customers' data, we have implemented robust measures across all our systems. We would like to commend John Doe (email: john.doe@example.com) from the IT department for his diligent work in enhancing our network security. Moving forward, we kindly remind everyone to strictly adhere to our data protection policies and guidelines. Additionally, if you come across any potential security risks or incidents, please report them immediately to our dedicated team at security@example.com.\n",
      "\n",
      "HR Updates and Employee Benefits\n",
      "Recently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: michael.johnson@example.com).\n",
      "\n",
      "Marketing Initiatives and Campaigns\n",
      "Our marketing team has been actively working on developing new strategies to increase brand awareness and drive customer engagement. We would like to thank Sarah Thompson (phone: 415-555-1234) for her exceptional efforts in managing our social media platforms. Sarah has successfully increased our follower base by 20% in the past month alone. Moreover, please mark your calendars for the upcoming product launch event on July 15th. We encourage all team members to attend and support this exciting milestone for our company.\n",
      "\n",
      "Research and Development Projects\n",
      "In our pursuit of innovation, our research and development department has been working tirelessly on various projects. I would like to acknowledge the exceptional work of David Rodriguez (email: david.rodriguez@example.com) in his role as project lead. David's contributions to the development of our cutting-edge technology have been instrumental. Furthermore, we would like to remind everyone to share their ideas and suggestions for potential new projects during our monthly R&D brainstorming session, scheduled for July 10th.\n",
      "\n",
      "Please treat the information in this document with utmost confidentiality and ensure that it is not shared with unauthorized individuals. If you have any questions or concerns regarding the topics discussed, please do not hesitate to reach out to me directly.\n",
      "\n",
      "Thank you for your attention, and let's continue to work together to achieve our goals.\n",
      "\n",
      "Best regards,\n",
      "\n",
      "Jason Fan\n",
      "Cofounder & CEO\n",
      "Psychic\n",
      "jason@psychic.dev\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
      "\u001b[36;1m\u001b[1;3m[Generated with ChatGPT]\n",
      "\n",
      "Confidential Document - For Internal Use Only\n",
      "\n",
      "Date: July 1, 2023\u001b[0m\n",
      "\u001b[33;1m\u001b[1;3m这是一份标注为“机密文件 - 仅供内部使用”的文档，日期为2023年7月1日。文档内容未提供具体信息，因此总结仅限于其标题和日期。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'这是一份标注为“机密文件 - 仅供内部使用”的文档，日期为2023年7月1日。文档内容未提供具体信息，因此总结仅限于其标题和日期。'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(letters_text_trans)\n",
    "\n",
    "simple_chain.run(letters_text_trans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import  PyPDFLoader\n",
    "\n",
    "class ChatPdf():\n",
    "    def getFile(file_path):\n",
    "        try:\n",
    "            #读取文件\n",
    "            loader = PyPDFLoader(file_path)\n",
    "            return loader.load()\n",
    "        except Exception as e:\n",
    "            print(f'Error: {e}')\n",
    "            return None\n",
    "\n",
    "pdf_doc = ChatPdf.getFile('./file/gdbs.pdf')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(metadata={'source': './file/gdbs.pdf', 'page': 0}, page_content='准备 java环境 \\n\\xa0\\n创建 mysql容器 \\n initdb.sql \\n mysql8.tar \\n\\xa0\\n apache-tomcat-9.0.58.tar.gz \\ntomcat操作 \\n解压 tomcat压缩包 \\n启动服务： ./apache-tomcat-9.0.58/bin/startup.sh\\n关闭服务： ./apache-tomcat-9.0.58/bin/shutdown.sh\\n查看日志： tail ./apache-tomcat-9.0.58/logs/catalina.out -f\\n访问地址： http://ip:8090/building\\nsudo apt-get install openjdk-8-jdk\\njava --version \\xa0# 检验 jdk 是否安装成功\\nsudo apt-get install redis-server \\xa0# 安装 reids\\n# 加载 mysql8 镜像\\nsudo docker load -i mysql8.tar \\nsudo docker run --name mysql8 \\xa0--restart=always \\xa0 --network host \\xa0 -v \\n/usr/mysql/conf:/etc/mysql/conf.d \\xa0 \\xa0 -v /usr/mysql/data2:/var/lib/mysql \\xa0 -e \\nMYSQL_ROOT_PASSWORD=\"root\" \\xa0 \\xa0-e TZ=Asia/Shanghai \\xa0-d mysql:8.0 --lower-case-\\ntable-names=1\\n# 安装客户端\\nsudo apt install mysql-client-core-5.7\\n# 初始化数据库  \\xa0 注意 < 后为 sql 文件路径  回车后输入密码  root\\nmysql -u root -p < /home/sk/java/mysql-init/initdb.sql')]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pdf_doc "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'准备 java环境 \\n\\xa0\\n创建 mysql容器 \\n initdb.sql \\n mysql8.tar \\n\\xa0\\n apache-tomcat-9.0.58.tar.gz \\ntomcat操作 \\n解压 tomcat压缩包 \\n启动服务： ./apache-tomcat-9.0.58/bin/startup.sh\\n关闭服务： ./apache-tomcat-9.0.58/bin/shutdown.sh\\n查看日志： tail ./apache-tomcat-9.0.58/logs/catalina.out -f\\n访问地址： http://ip:8090/building\\nsudo apt-get install openjdk-8-jdk\\njava --version \\xa0# 检验 jdk 是否安装成功\\nsudo apt-get install redis-server \\xa0# 安装 reids\\n# 加载 mysql8 镜像\\nsudo docker load -i mysql8.tar \\nsudo docker run --name mysql8 \\xa0--restart=always \\xa0 --network host \\xa0 -v \\n/usr/mysql/conf:/etc/mysql/conf.d \\xa0 \\xa0 -v /usr/mysql/data2:/var/lib/mysql \\xa0 -e \\nMYSQL_ROOT_PASSWORD=\"root\" \\xa0 \\xa0-e TZ=Asia/Shanghai \\xa0-d mysql:8.0 --lower-case-\\ntable-names=1\\n# 安装客户端\\nsudo apt install mysql-client-core-5.7\\n# 初始化数据库  \\xa0 注意 < 后为 sql 文件路径  回车后输入密码  root\\nmysql -u root -p < /home/sk/java/mysql-init/initdb.sql'"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pdf_doc_content = pdf_doc[0].page_content\n",
    "pdf_doc_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
      "\u001b[36;1m\u001b[1;3m准备 java环境 \n",
      " \n",
      "创建 mysql容器 \n",
      " initdb.sql \n",
      " mysql8.tar \n",
      " \n",
      " apache-tomcat-9.0.58.tar.gz \n",
      "tomcat操作 \n",
      "解压 tomcat压缩包 \n",
      "启动服务： ./apache-tomcat-9.0.58/bin/startup.sh\n",
      "关闭服务： ./apache-tomcat-9.0.58/bin/shutdown.sh\n",
      "查看日志： tail ./apache-tomcat-9.0.58/logs/catalina.out -f\n",
      "访问地址： http://ip:8090/building\n",
      "sudo apt-get install openjdk-8-jdk\n",
      "java --version  # 检验 jdk 是否安装成功\n",
      "sudo apt-get install redis-server  # 安装 reids\n",
      "# 加载 mysql8 镜像\n",
      "sudo docker load -i mysql8.tar \n",
      "sudo docker run --name mysql8  --restart=always   --network host   -v \n",
      "/usr/mysql/conf:/etc/mysql/conf.d     -v /usr/mysql/data2:/var/lib/mysql   -e \n",
      "MYSQL_ROOT_PASSWORD=\"root\"    -e TZ=Asia/Shanghai  -d mysql:8.0 --lower-case-\n",
      "table-names=1\n",
      "# 安装客户端\n",
      "sudo apt install mysql-client-core-5.7\n",
      "# 初始化数据库    注意 < 后为 sql 文件路径  回车后输入密码  root\n",
      "mysql -u root -p < /home/sk/java/mysql-init/initdb.sql\u001b[0m\n",
      "\u001b[33;1m\u001b[1;3m本文档描述了如何准备Java环境、创建MySQL容器、安装和操作Tomcat服务器、安装Redis、加载MySQL镜像、初始化数据库等步骤。具体步骤如下：\n",
      "\n",
      "1. **准备Java环境**：\n",
      "   - 安装OpenJDK 8：`sudo apt-get install openjdk-8-jdk`\n",
      "   - 验证JDK安装：`java --version`\n",
      "\n",
      "2. **创建MySQL容器**：\n",
      "   - 加载MySQL 8镜像：`sudo docker load -i mysql8.tar`\n",
      "   - 运行MySQL容器：`sudo docker run --name mysql8 --restart=always --network host -v /usr/mysql/conf:/etc/mysql/conf.d -v /usr/mysql/data2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=\"root\" -e TZ=Asia/Shanghai -d mysql:8.0 --lower-case-table-names=1`\n",
      "   - 安装MySQL客户端：`sudo apt install mysql-client-core-5.7`\n",
      "   - 初始化数据库：`mysql -u root -p < /home/sk/java/mysql-init/initdb.sql`\n",
      "\n",
      "3. **Tomcat操作**：\n",
      "   - 解压Tomcat压缩包：`apache-tomcat-9.0.58.tar.gz`\n",
      "   - 启动Tomcat服务：`./apache-tomcat-9.0.58/bin/startup.sh`\n",
      "   - 关闭Tomcat服务：`./apache-tomcat-9.0.58/bin/shutdown.sh`\n",
      "   - 查看日志：`tail ./apache-tomcat-9.0.58/logs/catalina.out -f`\n",
      "   - 访问地址：`http://ip:8090/building`\n",
      "\n",
      "4. **安装Redis**：\n",
      "   - 安装Redis服务器：`sudo apt-get install redis-server`\n",
      "\n",
      "通过这些步骤，可以成功搭建Java开发环境、配置MySQL数据库、部署Tomcat服务器以及安装Redis缓存服务。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'本文档描述了如何准备Java环境、创建MySQL容器、安装和操作Tomcat服务器、安装Redis、加载MySQL镜像、初始化数据库等步骤。具体步骤如下：\\n\\n1. **准备Java环境**：\\n   - 安装OpenJDK 8：`sudo apt-get install openjdk-8-jdk`\\n   - 验证JDK安装：`java --version`\\n\\n2. **创建MySQL容器**：\\n   - 加载MySQL 8镜像：`sudo docker load -i mysql8.tar`\\n   - 运行MySQL容器：`sudo docker run --name mysql8 --restart=always --network host -v /usr/mysql/conf:/etc/mysql/conf.d -v /usr/mysql/data2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=\"root\" -e TZ=Asia/Shanghai -d mysql:8.0 --lower-case-table-names=1`\\n   - 安装MySQL客户端：`sudo apt install mysql-client-core-5.7`\\n   - 初始化数据库：`mysql -u root -p < /home/sk/java/mysql-init/initdb.sql`\\n\\n3. **Tomcat操作**：\\n   - 解压Tomcat压缩包：`apache-tomcat-9.0.58.tar.gz`\\n   - 启动Tomcat服务：`./apache-tomcat-9.0.58/bin/startup.sh`\\n   - 关闭Tomcat服务：`./apache-tomcat-9.0.58/bin/shutdown.sh`\\n   - 查看日志：`tail ./apache-tomcat-9.0.58/logs/catalina.out -f`\\n   - 访问地址：`http://ip:8090/building`\\n\\n4. **安装Redis**：\\n   - 安装Redis服务器：`sudo apt-get install redis-server`\\n\\n通过这些步骤，可以成功搭建Java开发环境、配置MySQL数据库、部署Tomcat服务器以及安装Redis缓存服务。'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "simple_chain.run(pdf_doc_content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 链的运行方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
      "\u001b[36;1m\u001b[1;3m准备 java环境 \n",
      " \n",
      "创建 mysql容器 \n",
      " initdb.sql \n",
      " mysql8.tar \n",
      " \n",
      " apache-tomcat-9.0.58.tar.gz \n",
      "tomcat操作 \n",
      "解压 tomcat压缩包 \n",
      "启动服务： ./apache-tomcat-9.0.58/bin/startup.sh\n",
      "关闭服务： ./apache-tomcat-9.0.58/bin/shutdown.sh\n",
      "查看日志： tail ./apache-tomcat-9.0.58/logs/catalina.out -f\n",
      "访问地址： http://ip:8090/building\n",
      "sudo apt-get install openjdk-8-jdk\n",
      "java --version  # 检验 jdk 是否安装成功\n",
      "sudo apt-get install redis-server  # 安装 reids\n",
      "# 加载 mysql8 镜像\n",
      "sudo docker load -i mysql8.tar \n",
      "sudo docker run --name mysql8  --restart=always   --network host   -v \n",
      "/usr/mysql/conf:/etc/mysql/conf.d     -v /usr/mysql/data2:/var/lib/mysql   -e \n",
      "MYSQL_ROOT_PASSWORD=\"root\"    -e TZ=Asia/Shanghai  -d mysql:8.0 --lower-case-\n",
      "table-names=1\n",
      "# 安装客户端\n",
      "sudo apt install mysql-client-core-5.7\n",
      "# 初始化数据库    注意 < 后为 sql 文件路径  回车后输入密码  root\n",
      "mysql -u root -p < /home/sk/java/mysql-init/initdb.sql\u001b[0m\n",
      "\u001b[33;1m\u001b[1;3m本文档主要描述了如何准备Java环境、创建MySQL容器、安装和操作Tomcat服务器、安装Redis以及初始化MySQL数据库的步骤。具体步骤如下：\n",
      "\n",
      "1. **准备Java环境**：\n",
      "   - 安装OpenJDK 8：`sudo apt-get install openjdk-8-jdk`\n",
      "   - 验证JDK安装：`java --version`\n",
      "\n",
      "2. **创建MySQL容器**：\n",
      "   - 加载MySQL 8镜像：`sudo docker load -i mysql8.tar`\n",
      "   - 运行MySQL容器：`sudo docker run --name mysql8 --restart=always --network host -v /usr/mysql/conf:/etc/mysql/conf.d -v /usr/mysql/data2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=\"root\" -e TZ=Asia/Shanghai -d mysql:8.0 --lower-case-table-names=1`\n",
      "   - 安装MySQL客户端：`sudo apt install mysql-client-core-5.7`\n",
      "   - 初始化数据库：`mysql -u root -p < /home/sk/java/mysql-init/initdb.sql`\n",
      "\n",
      "3. **安装和操作Tomcat服务器**：\n",
      "   - 解压Tomcat压缩包：`apache-tomcat-9.0.58.tar.gz`\n",
      "   - 启动Tomcat服务：`./apache-tomcat-9.0.58/bin/startup.sh`\n",
      "   - 关闭Tomcat服务：`./apache-tomcat-9.0.58/bin/shutdown.sh`\n",
      "   - 查看Tomcat日志：`tail ./apache-tomcat-9.0.58/logs/catalina.out -f`\n",
      "   - 访问Tomcat服务：`http://ip:8090/building`\n",
      "\n",
      "4. **安装Redis**：\n",
      "   - 安装Redis服务器：`sudo apt-get install redis-server`\n",
      "\n",
      "通过这些步骤，可以成功搭建Java开发环境、配置MySQL数据库、部署Tomcat服务器以及安装Redis缓存服务。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'本文档主要描述了如何准备Java环境、创建MySQL容器、安装和操作Tomcat服务器、安装Redis以及初始化MySQL数据库的步骤。具体步骤如下：\\n\\n1. **准备Java环境**：\\n   - 安装OpenJDK 8：`sudo apt-get install openjdk-8-jdk`\\n   - 验证JDK安装：`java --version`\\n\\n2. **创建MySQL容器**：\\n   - 加载MySQL 8镜像：`sudo docker load -i mysql8.tar`\\n   - 运行MySQL容器：`sudo docker run --name mysql8 --restart=always --network host -v /usr/mysql/conf:/etc/mysql/conf.d -v /usr/mysql/data2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=\"root\" -e TZ=Asia/Shanghai -d mysql:8.0 --lower-case-table-names=1`\\n   - 安装MySQL客户端：`sudo apt install mysql-client-core-5.7`\\n   - 初始化数据库：`mysql -u root -p < /home/sk/java/mysql-init/initdb.sql`\\n\\n3. **安装和操作Tomcat服务器**：\\n   - 解压Tomcat压缩包：`apache-tomcat-9.0.58.tar.gz`\\n   - 启动Tomcat服务：`./apache-tomcat-9.0.58/bin/startup.sh`\\n   - 关闭Tomcat服务：`./apache-tomcat-9.0.58/bin/shutdown.sh`\\n   - 查看Tomcat日志：`tail ./apache-tomcat-9.0.58/logs/catalina.out -f`\\n   - 访问Tomcat服务：`http://ip:8090/building`\\n\\n4. **安装Redis**：\\n   - 安装Redis服务器：`sudo apt-get install redis-server`\\n\\n通过这些步骤，可以成功搭建Java开发环境、配置MySQL数据库、部署Tomcat服务器以及安装Redis缓存服务。'"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simple_chain.run(pdf_doc_content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
      "\u001b[36;1m\u001b[1;3m准备 java环境 \n",
      " \n",
      "创建 mysql容器 \n",
      " initdb.sql \n",
      " mysql8.tar \n",
      " \n",
      " apache-tomcat-9.0.58.tar.gz \n",
      "tomcat操作 \n",
      "解压 tomcat压缩包 \n",
      "启动服务： ./apache-tomcat-9.0.58/bin/startup.sh\n",
      "关闭服务： ./apache-tomcat-9.0.58/bin/shutdown.sh\n",
      "查看日志： tail ./apache-tomcat-9.0.58/logs/catalina.out -f\n",
      "访问地址： http://ip:8090/building\n",
      "sudo apt-get install openjdk-8-jdk\n",
      "java --version  # 检验 jdk 是否安装成功\n",
      "sudo apt-get install redis-server  # 安装 reids\n",
      "# 加载 mysql8 镜像\n",
      "sudo docker load -i mysql8.tar \n",
      "sudo docker run --name mysql8  --restart=always   --network host   -v \n",
      "/usr/mysql/conf:/etc/mysql/conf.d     -v /usr/mysql/data2:/var/lib/mysql   -e \n",
      "MYSQL_ROOT_PASSWORD=\"root\"    -e TZ=Asia/Shanghai  -d mysql:8.0 --lower-case-\n",
      "table-names=1\n",
      "# 安装客户端\n",
      "sudo apt install mysql-client-core-5.7\n",
      "# 初始化数据库    注意 < 后为 sql 文件路径  回车后输入密码  root\n",
      "mysql -u root -p < /home/sk/java/mysql-init/initdb.sql\u001b[0m\n",
      "\u001b[33;1m\u001b[1;3m本文档描述了如何准备Java环境、创建MySQL容器、安装和操作Tomcat服务器、安装Redis、加载MySQL镜像、初始化数据库等步骤。具体包括：\n",
      "\n",
      "1. **Java环境准备**：\n",
      "   - 安装OpenJDK 8：`sudo apt-get install openjdk-8-jdk`\n",
      "   - 验证JDK安装：`java --version`\n",
      "\n",
      "2. **MySQL容器创建**：\n",
      "   - 加载MySQL 8镜像：`sudo docker load -i mysql8.tar`\n",
      "   - 运行MySQL容器：`sudo docker run`命令配置了容器名称、网络、数据卷、时区等参数。\n",
      "   - 安装MySQL客户端：`sudo apt install mysql-client-core-5.7`\n",
      "   - 初始化数据库：`mysql -u root -p < /home/sk/java/mysql-init/initdb.sql`\n",
      "\n",
      "3. **Tomcat操作**：\n",
      "   - 解压Tomcat压缩包：`apache-tomcat-9.0.58.tar.gz`\n",
      "   - 启动Tomcat服务：`./apache-tomcat-9.0.58/bin/startup.sh`\n",
      "   - 关闭Tomcat服务：`./apache-tomcat-9.0.58/bin/shutdown.sh`\n",
      "   - 查看日志：`tail ./apache-tomcat-9.0.58/logs/catalina.out -f`\n",
      "   - 访问地址：`http://ip:8090/building`\n",
      "\n",
      "4. **Redis安装**：\n",
      "   - 安装Redis服务器：`sudo apt-get install redis-server`\n",
      "\n",
      "通过这些步骤，可以完成Java开发环境的搭建、MySQL数据库的部署、Tomcat服务器的启动与管理，以及Redis的安装。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '准备 java环境 \\n\\xa0\\n创建 mysql容器 \\n initdb.sql \\n mysql8.tar \\n\\xa0\\n apache-tomcat-9.0.58.tar.gz \\ntomcat操作 \\n解压 tomcat压缩包 \\n启动服务： ./apache-tomcat-9.0.58/bin/startup.sh\\n关闭服务： ./apache-tomcat-9.0.58/bin/shutdown.sh\\n查看日志： tail ./apache-tomcat-9.0.58/logs/catalina.out -f\\n访问地址： http://ip:8090/building\\nsudo apt-get install openjdk-8-jdk\\njava --version \\xa0# 检验 jdk 是否安装成功\\nsudo apt-get install redis-server \\xa0# 安装 reids\\n# 加载 mysql8 镜像\\nsudo docker load -i mysql8.tar \\nsudo docker run --name mysql8 \\xa0--restart=always \\xa0 --network host \\xa0 -v \\n/usr/mysql/conf:/etc/mysql/conf.d \\xa0 \\xa0 -v /usr/mysql/data2:/var/lib/mysql \\xa0 -e \\nMYSQL_ROOT_PASSWORD=\"root\" \\xa0 \\xa0-e TZ=Asia/Shanghai \\xa0-d mysql:8.0 --lower-case-\\ntable-names=1\\n# 安装客户端\\nsudo apt install mysql-client-core-5.7\\n# 初始化数据库  \\xa0 注意 < 后为 sql 文件路径  回车后输入密码  root\\nmysql -u root -p < /home/sk/java/mysql-init/initdb.sql',\n",
       " 'output': '本文档描述了如何准备Java环境、创建MySQL容器、安装和操作Tomcat服务器、安装Redis、加载MySQL镜像、初始化数据库等步骤。具体包括：\\n\\n1. **Java环境准备**：\\n   - 安装OpenJDK 8：`sudo apt-get install openjdk-8-jdk`\\n   - 验证JDK安装：`java --version`\\n\\n2. **MySQL容器创建**：\\n   - 加载MySQL 8镜像：`sudo docker load -i mysql8.tar`\\n   - 运行MySQL容器：`sudo docker run`命令配置了容器名称、网络、数据卷、时区等参数。\\n   - 安装MySQL客户端：`sudo apt install mysql-client-core-5.7`\\n   - 初始化数据库：`mysql -u root -p < /home/sk/java/mysql-init/initdb.sql`\\n\\n3. **Tomcat操作**：\\n   - 解压Tomcat压缩包：`apache-tomcat-9.0.58.tar.gz`\\n   - 启动Tomcat服务：`./apache-tomcat-9.0.58/bin/startup.sh`\\n   - 关闭Tomcat服务：`./apache-tomcat-9.0.58/bin/shutdown.sh`\\n   - 查看日志：`tail ./apache-tomcat-9.0.58/logs/catalina.out -f`\\n   - 访问地址：`http://ip:8090/building`\\n\\n4. **Redis安装**：\\n   - 安装Redis服务器：`sudo apt-get install redis-server`\\n\\n通过这些步骤，可以完成Java开发环境的搭建、MySQL数据库的部署、Tomcat服务器的启动与管理，以及Redis的安装。'}"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simple_chain(pdf_doc_content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 预制链\n",
    "\n",
    "https://smith.langchain.com/hub/rlm/rag-prompt  \n",
    "该网站上有大量的预制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\1701475839.py:3: LangChainDeprecationWarning: This function is deprecated and will be removed in langchain 1.0. At that point chains must be imported from their respective modules.\n",
      "  chain = load_chain(\"lc://chains/llm-math/chain.json\")\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "Loading from the deprecated github-based Hub is no longer supported. Please use the new LangChain Hub at https://smith.langchain.com/hub instead.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[75], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mlangchain\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mchains\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m load_chain\n\u001b[1;32m----> 3\u001b[0m chain \u001b[38;5;241m=\u001b[39m \u001b[43mload_chain\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mlc://chains/llm-math/chain.json\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m(chain\u001b[38;5;241m.\u001b[39mrun(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m2+6等于几?\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n",
      "File \u001b[1;32mc:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:182\u001b[0m, in \u001b[0;36mdeprecated.<locals>.deprecate.<locals>.warning_emitting_wrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    180\u001b[0m     warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m    181\u001b[0m     emit_warning()\n\u001b[1;32m--> 182\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m wrapped(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32mc:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\langchain\\chains\\loading.py:685\u001b[0m, in \u001b[0;36mload_chain\u001b[1;34m(path, **kwargs)\u001b[0m\n\u001b[0;32m    683\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Unified method for loading a chain from LangChainHub or local fs.\"\"\"\u001b[39;00m\n\u001b[0;32m    684\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(path, \u001b[38;5;28mstr\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m path\u001b[38;5;241m.\u001b[39mstartswith(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlc://\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m--> 685\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[0;32m    686\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLoading from the deprecated github-based Hub is no longer supported. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    687\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease use the new LangChain Hub at https://smith.langchain.com/hub \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    688\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minstead.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    689\u001b[0m     )\n\u001b[0;32m    690\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _load_chain_from_file(path, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "\u001b[1;31mRuntimeError\u001b[0m: Loading from the deprecated github-based Hub is no longer supported. Please use the new LangChain Hub at https://smith.langchain.com/hub instead."
     ]
    }
   ],
   "source": [
    "from langchain.chains import load_chain\n",
    "# \"lc://chains/llm-math/chain.json\" 属于预制链\n",
    "chain = load_chain(\"lc://chains/llm-math/chain.json\")\n",
    "\n",
    "print(chain.run(\"2+6等于几?\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 自定义链\n",
    "适合复杂的任务，灵活性和复用性高，支持自定义逻辑和回调，但代码复杂度较高。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List, Dict, Any, Optional\n",
    "from langchain.callbacks.manager import (\n",
    "    CallbackManagerForChainRun\n",
    ")\n",
    "from langchain.chains.base import  Chain\n",
    "from langchain.prompts.base import BasePromptTemplate\n",
    "from langchain.base_language import  BaseLanguageModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "class xhs_article_chain(Chain):\n",
    "    \"\"\"小红书文案生成链\"\"\"\n",
    "    prompt: BasePromptTemplate\n",
    "    llm: BaseLanguageModel\n",
    "    out_key: str = \"text\"\n",
    "\n",
    "    \"\"\"获取当前prompt需要得所有键\"\"\"\n",
    "    @property\n",
    "    def input_keys(self) -> List[str]:\n",
    "        return self.prompt.input_variables\n",
    "\n",
    "    \"\"\"获取当前prompt需要得到的输出键\"\"\"\n",
    "    @property\n",
    "    def output_keys(self) -> List[str]:\n",
    "        \"\"\"将始终返回text键\"\"\"\n",
    "        return [self.out_key]\n",
    "    \n",
    "    @property\n",
    "    def _chain_type(self) -> str:\n",
    "        \"\"\"链类型\"\"\"\n",
    "        return \"wiki_article_chain\"\n",
    "    \n",
    "    def _call(self, \n",
    "              inputs:Dict[str, Any] ,\n",
    "              run_manager : Optional[CallbackManagerForChainRun] = None, \n",
    "              ) -> Dict[str, Any]:\n",
    "            \n",
    "            prompt_value = self.prompt.format_prompt(**inputs)\n",
    "            print(\"prompt_value:\",prompt_value)\n",
    "            response = self.llm.generate_prompt(\n",
    "                [prompt_value],callbacks=run_manager.get_child() if run_manager else None\n",
    "            )\n",
    "            print(\"response:\",response)\n",
    "            if run_manager:\n",
    "                run_manager.on_text(\"wiki article is written\")\n",
    "\n",
    "            return {self.out_key:response.generations[0][0].text}\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import  PromptTemplate\n",
    "chain = xhs_article_chain(\n",
    "    prompt = PromptTemplate(template=\"写一篇关于{topic}的小红书文章\",\n",
    "                             input_variables=['topic']),\n",
    "                               llm=llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "prompt_value: text='写一篇关于冰红茶的小红书文章'\n",
      "response: generations=[[ChatGeneration(text='## 冰红茶，我的夏日续命水！🍹\\n\\n姐妹们！我真的要给你们安利一下我的夏日续命水——冰红茶！🍹\\n\\n最近天气热到爆炸，出门五分钟，流汗两小时🥵，真的急需一杯冰爽的饮料来续命！而冰红茶，简直就是我的救星！✨\\n\\n**冰红茶的好处，真的太多了！**\\n\\n* **解暑降温：** 一口下去，冰冰凉凉，瞬间感觉整个人都活过来了！🧊\\n* **提神醒脑：** 红茶本身就含有咖啡因，可以提神醒脑，让你在炎炎夏日也能保持活力！💪\\n* **美容养颜：** 红茶富含抗氧化剂，可以帮助抵抗自由基，延缓衰老，让你越喝越美丽！💃\\n* **促进消化：** 饭后喝一杯冰红茶，可以帮助消化，让你轻松无负担！🍽️\\n\\n**我的独家冰红茶配方：**\\n\\n* 红茶包 2 个\\n* 开水 500ml\\n* 冰糖 适量\\n* 柠檬片 2-3 片\\n* 冰块 适量\\n\\n**步骤：**\\n\\n1. 将红茶包放入杯中，倒入开水，浸泡 5 分钟。\\n2. 取出茶包，加入冰糖，搅拌至融化。\\n3. 加入柠檬片和冰块，搅拌均匀即可。\\n\\n**小贴士：**\\n\\n* 可以根据个人口味调整冰糖的用量。\\n* 可以加入其他水果，如草莓、蓝莓等，增加风味。\\n* 可以提前将红茶放入冰箱冷藏，喝起来更冰爽！\\n\\n**姐妹们，赶紧试试吧！** 这个夏天，让我们一起用冰红茶续命，清凉一夏！🍹\\n\\n#冰红茶 #夏日续命水 #解暑降温 #提神醒脑 #美容养颜 #促进消化', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='## 冰红茶，我的夏日续命水！🍹\\n\\n姐妹们！我真的要给你们安利一下我的夏日续命水——冰红茶！🍹\\n\\n最近天气热到爆炸，出门五分钟，流汗两小时🥵，真的急需一杯冰爽的饮料来续命！而冰红茶，简直就是我的救星！✨\\n\\n**冰红茶的好处，真的太多了！**\\n\\n* **解暑降温：** 一口下去，冰冰凉凉，瞬间感觉整个人都活过来了！🧊\\n* **提神醒脑：** 红茶本身就含有咖啡因，可以提神醒脑，让你在炎炎夏日也能保持活力！💪\\n* **美容养颜：** 红茶富含抗氧化剂，可以帮助抵抗自由基，延缓衰老，让你越喝越美丽！💃\\n* **促进消化：** 饭后喝一杯冰红茶，可以帮助消化，让你轻松无负担！🍽️\\n\\n**我的独家冰红茶配方：**\\n\\n* 红茶包 2 个\\n* 开水 500ml\\n* 冰糖 适量\\n* 柠檬片 2-3 片\\n* 冰块 适量\\n\\n**步骤：**\\n\\n1. 将红茶包放入杯中，倒入开水，浸泡 5 分钟。\\n2. 取出茶包，加入冰糖，搅拌至融化。\\n3. 加入柠檬片和冰块，搅拌均匀即可。\\n\\n**小贴士：**\\n\\n* 可以根据个人口味调整冰糖的用量。\\n* 可以加入其他水果，如草莓、蓝莓等，增加风味。\\n* 可以提前将红茶放入冰箱冷藏，喝起来更冰爽！\\n\\n**姐妹们，赶紧试试吧！** 这个夏天，让我们一起用冰红茶续命，清凉一夏！🍹\\n\\n#冰红茶 #夏日续命水 #解暑降温 #提神醒脑 #美容养颜 #促进消化', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 387, 'prompt_tokens': 12, 'total_tokens': 399, 'completion_tokens_details': None, 'prompt_tokens_details': None, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 12}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_3a5770e1b4', 'finish_reason': 'stop', 'logprobs': None}, id='run-01f881b7-3285-4514-a6ae-fa9b46044c47-0', usage_metadata={'input_tokens': 12, 'output_tokens': 387, 'total_tokens': 399, 'input_token_details': {}, 'output_token_details': {}}))]] llm_output={'token_usage': {'completion_tokens': 387, 'prompt_tokens': 12, 'total_tokens': 399, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 12}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_3a5770e1b4'} run=[RunInfo(run_id=UUID('01f881b7-3285-4514-a6ae-fa9b46044c47'))] type='LLMResult'\n",
      "**冰红茶**\n",
      "\n",
      "## 冰红茶，我的夏日续命水！🍹\n",
      "\n",
      "姐妹们！我真的要给你们安利一下我的夏日续命水——冰红茶！🍹\n",
      "\n",
      "最近天气热到爆炸，出门五分钟，流汗两小时🥵，真的急需一杯冰爽的饮料来续命！而冰红茶，简直就是我的救星！✨\n",
      "\n",
      "**冰红茶的好处，真的太多了！**\n",
      "\n",
      "* **解暑降温：** 一口下去，冰冰凉凉，瞬间感觉整个人都活过来了！🧊\n",
      "* **提神醒脑：** 红茶本身就含有咖啡因，可以提神醒脑，让你在炎炎夏日也能保持活力！💪\n",
      "* **美容养颜：** 红茶富含抗氧化剂，可以帮助抵抗自由基，延缓衰老，让你越喝越美丽！💃\n",
      "* **促进消化：** 饭后喝一杯冰红茶，可以帮助消化，让你轻松无负担！🍽️\n",
      "\n",
      "**我的独家冰红茶配方：**\n",
      "\n",
      "* 红茶包 2 个\n",
      "* 开水 500ml\n",
      "* 冰糖 适量\n",
      "* 柠檬片 2-3 片\n",
      "* 冰块 适量\n",
      "\n",
      "**步骤：**\n",
      "\n",
      "1. 将红茶包放入杯中，倒入开水，浸泡 5 分钟。\n",
      "2. 取出茶包，加入冰糖，搅拌至融化。\n",
      "3. 加入柠檬片和冰块，搅拌均匀即可。\n",
      "\n",
      "**小贴士：**\n",
      "\n",
      "* 可以根据个人口味调整冰糖的用量。\n",
      "* 可以加入其他水果，如草莓、蓝莓等，增加风味。\n",
      "* 可以提前将红茶放入冰箱冷藏，喝起来更冰爽！\n",
      "\n",
      "**姐妹们，赶紧试试吧！** 这个夏天，让我们一起用冰红茶续命，清凉一夏！🍹\n",
      "\n",
      "#冰红茶 #夏日续命水 #解暑降温 #提神醒脑 #美容养颜 #促进消化\n"
     ]
    }
   ],
   "source": [
    "def beautify_output(article: dict) -> str:\n",
    "    # 提取标题和正文\n",
    "    title = article.get(\"topic\", \"未知主题\")\n",
    "    content = article.get(\"text\", \"\")\n",
    "\n",
    "    # 美化标题\n",
    "    beautified_title = f\"**{title}**\\n\\n\"\n",
    "\n",
    "    # 美化正文\n",
    "    beautified_content = content.replace(\"**标题：**\", \"**标题：**\\n\")  # 标题换行\n",
    "    beautified_content = beautified_content.replace(\"**正文：**\", \"\")  # 去掉多余的“正文”标签\n",
    "    beautified_content = beautified_content.replace(\"**1.\", \"\\n**1.\")  # 小标题换行\n",
    "    beautified_content = beautified_content.replace(\"**2.\", \"\\n**2.\")  # 小标题换行\n",
    "    beautified_content = beautified_content.replace(\"**3.\", \"\\n**3.\")  # 小标题换行\n",
    "    beautified_content = beautified_content.replace(\"**4.\", \"\\n**4.\")  # 小标题换行\n",
    "    beautified_content = beautified_content.replace(\"**最后，\", \"\\n**最后，\")  # 最后部分换行\n",
    "    beautified_content = beautified_content.replace(\"**#\", \"\\n**#\")  # 标签换行\n",
    "    beautified_content = beautified_content.replace(\"**互动：**\", \"\\n**互动：**\\n\")  # 互动部分换行\n",
    "\n",
    "    # 组合美化后的内容\n",
    "    beautified_article = beautified_title + beautified_content\n",
    "\n",
    "    return beautified_article\n",
    "\n",
    "arti = chain(\"冰红茶\")\n",
    "\n",
    "print(beautify_output(arti))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文档处理链"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### stuffChain   \n",
    "最常见的文档链，将文档直接塞进prompt中，为LLM回答问题提供上下文资料，适合小文档场景"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. **Java环境**：安装JDK 8并验证。\n",
      "2. **MySQL容器**：加载MySQL 8镜像，创建并运行容器，初始化数据库。\n",
      "3. **Tomcat**：解压Tomcat，启动/关闭服务，查看日志，访问应用。\n",
      "4. **Redis**：安装Redis服务器。\n",
      "5. **MySQL客户端**：安装MySQL客户端并初始化数据库。\n"
     ]
    }
   ],
   "source": [
    "from langchain.chains.combine_documents.stuff import StuffDocumentsChain\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.document_loaders import  PyPDFLoader\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "loader = PyPDFLoader('./file/gdbs.pdf')\n",
    "\n",
    "prompt_template = \"\"\"对以下文字做尽可能的简洁总结:\n",
    "{text}\n",
    "简洁的总结:\"\"\"\n",
    "\n",
    "prompt = PromptTemplate(template=prompt_template, input_variables=[\"text\"])\n",
    "llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
    "\n",
    "stuff_chain = StuffDocumentsChain(llm_chain=llm_chain, document_variable_name='text')\n",
    "\n",
    "documents = loader.load()\n",
    "\n",
    "summary = stuff_chain.run(documents)\n",
    "print(summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### load_summarize_chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new StuffDocumentsChain chain...\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"准备 java环境 \n",
      " \n",
      "创建 mysql容器 \n",
      " initdb.sql \n",
      " mysql8.tar \n",
      " \n",
      " apache-tomcat-9.0.58.tar.gz \n",
      "tomcat操作 \n",
      "解压 tomcat压缩包 \n",
      "启动服务： ./apache-tomcat-9.0.58/bin/startup.sh\n",
      "关闭服务： ./apache-tomcat-9.0.58/bin/shutdown.sh\n",
      "查看日志： tail ./apache-tomcat-9.0.58/logs/catalina.out -f\n",
      "访问地址： http://ip:8090/building\n",
      "sudo apt-get install openjdk-8-jdk\n",
      "java --version  # 检验 jdk 是否安装成功\n",
      "sudo apt-get install redis-server  # 安装 reids\n",
      "# 加载 mysql8 镜像\n",
      "sudo docker load -i mysql8.tar \n",
      "sudo docker run --name mysql8  --restart=always   --network host   -v \n",
      "/usr/mysql/conf:/etc/mysql/conf.d     -v /usr/mysql/data2:/var/lib/mysql   -e \n",
      "MYSQL_ROOT_PASSWORD=\"root\"    -e TZ=Asia/Shanghai  -d mysql:8.0 --lower-case-\n",
      "table-names=1\n",
      "# 安装客户端\n",
      "sudo apt install mysql-client-core-5.7\n",
      "# 初始化数据库    注意 < 后为 sql 文件路径  回车后输入密码  root\n",
      "mysql -u root -p < /home/sk/java/mysql-init/initdb.sql\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "------------------------------------------\n",
      "The instructions outline steps to set up a Java environment, deploy a MySQL container, and configure Apache Tomcat. Key tasks include:\n",
      "\n",
      "1. **Java Setup**: Install OpenJDK 8 and verify installation.\n",
      "2. **MySQL Container**: Load a MySQL 8 image, run a container with specific configurations, and initialize the database using an SQL script.\n",
      "3. **Tomcat Setup**: Extract the Tomcat package, start/stop the service, and monitor logs.\n",
      "4. **Redis Installation**: Install Redis server.\n",
      "5. **MySQL Client**: Install the MySQL client for database management.\n",
      "\n",
      "The process involves using Docker for MySQL, managing Tomcat services, and ensuring proper environment configuration for Java and database operations.\n"
     ]
    }
   ],
   "source": [
    "from langchain.chains.summarize import load_summarize_chain\n",
    "chain = load_summarize_chain(\n",
    "    llm=llm,\n",
    "    chain_type=\"stuff\",\n",
    "    verbose=True,\n",
    "    )\n",
    "\n",
    "summary = chain.run(documents)\n",
    "print('------------------------------------------')\n",
    "print(summary)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### refine\n",
    "通过循环引用LLM，将文档不断投喂，并产生各种中间答案，适合逻辑有上下文关联的文档，不适合交叉引用的文档\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import  PromptTemplate\n",
    "from langchain.document_loaders import PyPDFLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.chains.summarize import load_summarize_chain\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "#load\n",
    "loader = PyPDFLoader(\"wfw.pdf\")\n",
    "docs = loader.load()\n",
    "#split\n",
    "text_split = CharacterTextSplitter.from_tiktoken_encoder(\n",
    "    chunk_size = 1000,\n",
    "    chunk_overlap=0\n",
    ")\n",
    "split_docs = text_split.split_documents(docs)\n",
    "prompt_template = \"\"\"对以下文字做简洁的总结:\n",
    "{text}\n",
    "简洁的总结:\"\"\"\n",
    "\n",
    "prompt = PromptTemplate.from_template(prompt_template)\n",
    "\n",
    "refine_template = (\n",
    "    \"你的任务是产生最终摘要\\n\"\n",
    "    \"我们已经提供了一个到某个特定点的现有回答:{existing_answer}\\n\"\n",
    "    \"我们有机会通过下面的一些更多上下文来完善现有的回答(仅在需要时使用).\\n\"\n",
    "    \"------------\\n\"\n",
    "    \"{text}\\n\"\n",
    "    \"------------\\n\"\n",
    "    \"根据新的上下文，用中文完善原始回答.\\n\"\n",
    "    \"如果上下文没有用处,返回原始回答.\"\n",
    ")\n",
    "\n",
    "\n",
    "refine_prompt = PromptTemplate.from_template(refine_template)\n",
    "\n",
    "chain = load_summarize_chain(\n",
    "    llm=llm,\n",
    "    chain_type=\"refine\",\n",
    "    question_prompt=prompt,\n",
    "    refine_prompt = refine_prompt,\n",
    "    return_intermediate_steps=True,\n",
    "    input_key = \"documents\",\n",
    "    output_key = \"output_text\",\n",
    ")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "传统单体架构开发、测试、部署快速，运维简单，但模块间耦合度高，难以独立部署。微服务架构通过将系统拆分为独立的服务单元，每个服务可独立设计、开发、测试和部署，服务间通过API接口交互。微服务的核心是“大拆小”，服务以进程形式存在，通过注册中心（如Nacos）实现服务发现和调用。前端通过网关（如Gateway）统一访问后端服务，实现前后端分离和接口的统一管理。\n",
      "\n",
      "需要注意的是，虽然有了微服务网关，但仍需要API网关。如果仅仅是微服务网关，那么它的流量请求代理只能到微服务的粒度，但是当有了API网关之后，整个接口的请求流量代理可以细化到一个个API服务接口的粒度，从而提供更精细的流量管理和控制。\n"
     ]
    }
   ],
   "source": [
    "result = chain({\"documents\":split_docs},return_only_outputs=True)\n",
    "print(result[\"output_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Map reduce\n",
    "先将每个文档或文档块分别投喂给LLM，并得到结果集（Map步骤），然后通过一个文档合并链，获得一个输出结果（Reduce步骤）\n",
    "![Alt Text](map.png)\n",
    "![Alt Text](reduce.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(metadata={'source': 'wfw.pdf', 'page': 0}, page_content='快速传统单体架构  \\n快速完成开发测试部署，运维简单 \\n微服务架构  \\n\\xa0\\n将传统单体业务架构系统，拆分为更细的粒度，每一个单位都是可以独立的进行设计、开发、测试、部 \\n署，每个服务都可以做到独立的自治，这个拆分出来的单位就是微服务 \\n同时每个拆分出来的单位之间，只能通过 Api接口进行协同或者交互 \\n\\xa0\\n微服务的核心就是   大拆小 \\n不同的服务是以进程的形式存在的 \\n传统的架构模式下，假如  在界面上看  有摄像头模块、警报记录模块、基础信息模块    ， \\n在   展现层   ，仍然可以看到三个独立的一级菜单，   在用户的角度，就好像是拆分出来的三个独立的模块 \\n或者组件 \\n但是一到了   逻辑层，就会发现所有的业务之间、组件之间   耦合度非常高，   有大量的交叉调用，完全没 \\n有办法去拆开进行独立部署，    同时整个系统都是使用一个数据库，  这就是传统的单体架构 \\n \\n微服务架构，在展现层其实还是一致的，但是在业务上就是大变样，   每个模块之间是独立的，  \\n但是、业务之间必然会存在一定的相互调用，那么此时就必然不可以直接通过跨 DB 进行调用，  只能、 \\n只能、只能通过 API接口调用 ------ 这里就涉及到了  cloud 微服务组件中的   fegin 组件了，该组件可以 \\n实现跨服务调用接口的目的，    \\n但是、想要调用对方服务中的接口，这可是跨服务调用，一个服务怎么能够知道所想要调用的对方暴露 \\n的接口地址   怎么才能知道呢？？？ \\n就需要有一个大前提，  能够相互发现对方服务的存在，  这里就引出了注册中心的重要性，每个服务都需 \\n要先在注册中心中进行注册， -------cloud 微服务组件中的   nacos 组件  就可以完美的充当  注册中心角色\\n \\n一个服务想要调用另外一个服务的接口，就会先从注册中心找到  对方服务中的接口地址，  然后在发起对 \\n接口的调用，  所以在微服务架构中  ，常常会谈所谓的去中心化架构，    接口之间本来是点对点的调用， \\n但是虽然有注册中心这个  只是用来管最基本的控制流的的存在， \\n就算注册中心宕机了，但是由于各个服务之间存在一些地址的缓存，多个微服务之间仍然可以发起组件 \\n之间接口的互相调用 \\n \\n此时，再讲目光转移到前端，前端无论界面展示的是怎么样，要做业务处理，就需要调用后端服务器中 \\n的代码，需要前端发起 http 的 API接口的调用，但是前端是无法感知到服务的存在的   再加上现在项目 \\n都是前后端分离的模式   ，所以一定要加上一层 ======gateway 网关，   通过网关，统一的对所有接口进 \\n行调用以及暴露，在这里给出前端可以访问的 http 访问地址，'), Document(metadata={'source': 'wfw.pdf', 'page': 1}, page_content='注意，虽然有了微服务网关，但仍需要 API网关 \\n如果仅仅是微服务网关，那么它的流量的请求代理，只能到微服务的粒度，但是当有了 API网关之后， \\n整个接口的请求流量代理，是可以到一个个 Api服务接口的粒度')]\n"
     ]
    }
   ],
   "source": [
    "from langchain.chains import MapReduceDocumentsChain\n",
    "from langchain.chains import ReduceDocumentsChain\n",
    "from langchain.chains.combine_documents.stuff import StuffDocumentsChain\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.document_loaders import PyPDFLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "#load pdf\n",
    "loader = PyPDFLoader(\"wfw.pdf\")\n",
    "docs = loader.load()\n",
    "#split text\n",
    "text_splitter = CharacterTextSplitter.from_tiktoken_encoder(\n",
    "    chunk_size=100,\n",
    "    chunk_overlap=0,\n",
    ")\n",
    "split_docs = text_splitter.split_documents(docs)\n",
    "print(split_docs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "#map chain\n",
    "map_template = \"\"\"对以下文字做简洁的总结:\n",
    "\"{content}\"\n",
    "简洁的总结:\"\"\"\n",
    "map_prompt = PromptTemplate.from_template(map_template)\n",
    "map_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=map_prompt,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\4052971258.py:16: LangChainDeprecationWarning: This class is deprecated. Please see the migration guide here for a recommended replacement: https://python.langchain.com/docs/versions/migrating_chains/map_reduce_chain/\n",
      "  reduce_final_chain = ReduceDocumentsChain(\n",
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\4052971258.py:24: LangChainDeprecationWarning: This class is deprecated. Please see the migration guide here for a recommended replacement: https://python.langchain.com/docs/versions/migrating_chains/map_reduce_chain/\n",
      "  map_reduce_chain = MapReduceDocumentsChain(\n"
     ]
    }
   ],
   "source": [
    "#reduce chain\n",
    "reduce_template = \"\"\"以下是一个摘要集合:\n",
    "{doc_summaries}\n",
    "将上述摘要与所有关键细节进行总结.\n",
    "总结:\"\"\"\n",
    "reduce_prompt = PromptTemplate.from_template(reduce_template)\n",
    "reduce_chain = LLMChain(\n",
    "    prompt=reduce_prompt,\n",
    "    llm=llm,\n",
    ")\n",
    "stuff_chain = StuffDocumentsChain(\n",
    "    llm_chain=reduce_chain,\n",
    "    document_variable_name=\"doc_summaries\",\n",
    ")\n",
    "\n",
    "reduce_final_chain = ReduceDocumentsChain(\n",
    "    combine_documents_chain=stuff_chain,\n",
    "    #超过4000个token就会切入到下一个stuff_chain\n",
    "    collapse_documents_chain=stuff_chain,\n",
    "    token_max=4000,\n",
    ")\n",
    "\n",
    "#map reduce chain\n",
    "map_reduce_chain = MapReduceDocumentsChain(\n",
    "    llm_chain=map_chain,\n",
    "    document_variable_name=\"content\",\n",
    "    reduce_documents_chain=reduce_final_chain,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**总结：**\n",
      "\n",
      "1. **传统单体架构**：\n",
      "   - **优点**：开发、测试、部署速度快，运维简单。\n",
      "   - **缺点**：模块间耦合度高，难以独立部署，通常使用单一数据库。\n",
      "\n",
      "2. **微服务架构**：\n",
      "   - **核心思想**：将单体系统拆分为多个独立的微服务，每个服务可独立设计、开发、测试和部署。\n",
      "   - **服务间交互**：通过API接口进行通信，服务以独立进程存在。\n",
      "   - **服务发现**：通过注册中心（如Nacos）相互发现，即使注册中心宕机，服务间仍可通过缓存地址进行调用。\n",
      "   - **前后端分离**：前端通过网关（如Gateway）统一访问后端服务。\n",
      "\n",
      "3. **微服务网关与API网关的区别**：\n",
      "   - **微服务网关**：只能代理到微服务粒度。\n",
      "   - **API网关**：可以代理到具体的API接口粒度，实现更细粒度的流量管理。\n",
      "\n",
      "**关键细节**：\n",
      "- 微服务架构通过“大拆小”实现服务的独立性和灵活性。\n",
      "- 服务间通过API调用（如Fegin）进行通信，确保系统的可扩展性和可维护性。\n",
      "- 即使注册中心宕机，服务间仍可通过缓存地址进行调用，提高系统的可靠性。\n",
      "- 前端通过网关统一访问后端服务，实现前后端分离，提升系统的可管理性和安全性。\n",
      "- 微服务网关和API网关的结合使用，可以实现从服务粒度到API接口粒度的全面流量管理。\n"
     ]
    }
   ],
   "source": [
    "summ = map_reduce_chain(split_docs)\n",
    "print(summ['output_text'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Map re-rank\n",
    "先将每个文档或文档块投喂给LLM,并对每个文档或文档块生成问题的答案进行打分，然后将打分最高的文档或文档块作为最终答案返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "from langchain.document_loaders import PyPDFLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.chains.qa_with_sources import load_qa_with_sources_chain\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "#load\n",
    "loader = PyPDFLoader(\"wfw.pdf\")\n",
    "docs = loader.load()\n",
    "#split\n",
    "text_splitter = CharacterTextSplitter.from_tiktoken_encoder(\n",
    "    chunk_size=200, chunk_overlap=0\n",
    ")\n",
    "split_docs = text_splitter.split_documents(docs)\n",
    "\n",
    "chain = load_qa_with_sources_chain(\n",
    "    llm=llm, \n",
    "    chain_type=\"map_rerank\", \n",
    "    metadata_keys=['source'], \n",
    "    return_intermediate_steps=True\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "verbose=False llm_chain=LLMChain(verbose=False, prompt=PromptTemplate(input_variables=['context', 'question'], input_types={}, output_parser=RegexParser(regex='(.*?)\\\\nScore: (\\\\d*)', output_keys=['answer', 'score']), partial_variables={}, template=\"Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\\n\\nIn addition to giving an answer, also return a score of how fully it answered the user's question. This should be in the following format:\\n\\nQuestion: [question here]\\nHelpful Answer: [answer here]\\nScore: [score between 0 and 100]\\n\\nHow to determine the score:\\n- Higher is a better answer\\n- Better responds fully to the asked question, with sufficient level of detail\\n- If you do not know the answer based on the context, that should be a score of 0\\n- Don't be overconfident!\\n\\nExample #1\\n\\nContext:\\n---------\\nApples are red\\n---------\\nQuestion: what color are apples?\\nHelpful Answer: red\\nScore: 100\\n\\nExample #2\\n\\nContext:\\n---------\\nit was night and the witness forgot his glasses. he was not sure if it was a sports car or an suv\\n---------\\nQuestion: what type was the car?\\nHelpful Answer: a sports car or an suv\\nScore: 60\\n\\nExample #3\\n\\nContext:\\n---------\\nPears are either red or orange\\n---------\\nQuestion: what color are apples?\\nHelpful Answer: This document does not answer the question\\nScore: 0\\n\\nBegin!\\n\\nContext:\\n---------\\n{context}\\n---------\\nQuestion: {question}\\nHelpful Answer:\"), llm=ChatOpenAI(client=<openai.resources.chat.completions.Completions object at 0x00000201ADFA63B0>, async_client=<openai.resources.chat.completions.AsyncCompletions object at 0x00000201ADFA56F0>, root_client=<openai.OpenAI object at 0x00000201AE2D1AE0>, root_async_client=<openai.AsyncOpenAI object at 0x00000201ADFA77C0>, model_name='deepseek-chat', model_kwargs={}, openai_api_key=SecretStr('**********'), openai_api_base='https://api.deepseek.com', max_tokens=1024), output_parser=StrOutputParser(), llm_kwargs={}) document_variable_name='context' rank_key='score' answer_key='answer' metadata_keys=['source'] return_intermediate_steps=True\n"
     ]
    }
   ],
   "source": [
    "print(chain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\langchain\\chains\\llm.py:369: UserWarning: The apply_and_parse method is deprecated, instead pass an output parser directly to LLMChain.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input_documents': [Document(metadata={'source': 'wfw.pdf', 'page': 0}, page_content='快速传统单体架构  \\n快速完成开发测试部署，运维简单 \\n微服务架构  \\n\\xa0\\n将传统单体业务架构系统，拆分为更细的粒度，每一个单位都是可以独立的进行设计、开发、测试、部 \\n署，每个服务都可以做到独立的自治，这个拆分出来的单位就是微服务 \\n同时每个拆分出来的单位之间，只能通过 Api接口进行协同或者交互 \\n\\xa0\\n微服务的核心就是   大拆小 \\n不同的服务是以进程的形式存在的 \\n传统的架构模式下，假如  在界面上看  有摄像头模块、警报记录模块、基础信息模块    ， \\n在   展现层   ，仍然可以看到三个独立的一级菜单，   在用户的角度，就好像是拆分出来的三个独立的模块 \\n或者组件 \\n但是一到了   逻辑层，就会发现所有的业务之间、组件之间   耦合度非常高，   有大量的交叉调用，完全没 \\n有办法去拆开进行独立部署，    同时整个系统都是使用一个数据库，  这就是传统的单体架构 \\n \\n微服务架构，在展现层其实还是一致的，但是在业务上就是大变样，   每个模块之间是独立的，  \\n但是、业务之间必然会存在一定的相互调用，那么此时就必然不可以直接通过跨 DB 进行调用，  只能、 \\n只能、只能通过 API接口调用 ------ 这里就涉及到了  cloud 微服务组件中的   fegin 组件了，该组件可以 \\n实现跨服务调用接口的目的，    \\n但是、想要调用对方服务中的接口，这可是跨服务调用，一个服务怎么能够知道所想要调用的对方暴露 \\n的接口地址   怎么才能知道呢？？？ \\n就需要有一个大前提，  能够相互发现对方服务的存在，  这里就引出了注册中心的重要性，每个服务都需 \\n要先在注册中心中进行注册， -------cloud 微服务组件中的   nacos 组件  就可以完美的充当  注册中心角色\\n \\n一个服务想要调用另外一个服务的接口，就会先从注册中心找到  对方服务中的接口地址，  然后在发起对 \\n接口的调用，  所以在微服务架构中  ，常常会谈所谓的去中心化架构，    接口之间本来是点对点的调用， \\n但是虽然有注册中心这个  只是用来管最基本的控制流的的存在， \\n就算注册中心宕机了，但是由于各个服务之间存在一些地址的缓存，多个微服务之间仍然可以发起组件 \\n之间接口的互相调用 \\n \\n此时，再讲目光转移到前端，前端无论界面展示的是怎么样，要做业务处理，就需要调用后端服务器中 \\n的代码，需要前端发起 http 的 API接口的调用，但是前端是无法感知到服务的存在的   再加上现在项目 \\n都是前后端分离的模式   ，所以一定要加上一层 ======gateway 网关，   通过网关，统一的对所有接口进 \\n行调用以及暴露，在这里给出前端可以访问的 http 访问地址，'),\n",
       "  Document(metadata={'source': 'wfw.pdf', 'page': 1}, page_content='注意，虽然有了微服务网关，但仍需要 API网关 \\n如果仅仅是微服务网关，那么它的流量的请求代理，只能到微服务的粒度，但是当有了 API网关之后， \\n整个接口的请求流量代理，是可以到一个个 Api服务接口的粒度')],\n",
       " 'question': 'what is this document talk about?answer by chinese',\n",
       " 'source': 'wfw.pdf',\n",
       " 'intermediate_steps': [{'answer': 'Helpful Answer: 本文档主要讨论了传统单体架构与微服务架构的区别。传统单体架构将所有功能模块集成在一个系统中，模块之间耦合度高，难以独立部署。而微服务架构将系统拆分为多个独立的服务，每个服务可以独立设计、开发、测试和部署，服务之间通过API接口进行交互。文档还介绍了微服务架构中的关键组件，如注册中心（如Nacos）和网关（如Gateway），以及它们的作用，例如服务发现和接口调用。  ',\n",
       "   'score': '95'},\n",
       "  {'answer': 'Helpful Answer: 本文档讨论了微服务网关和API网关的区别。微服务网关只能代理到微服务的粒度，而API网关可以代理到具体的API服务接口粒度。',\n",
       "   'score': '90'}],\n",
       " 'output_text': 'Helpful Answer: 本文档主要讨论了传统单体架构与微服务架构的区别。传统单体架构将所有功能模块集成在一个系统中，模块之间耦合度高，难以独立部署。而微服务架构将系统拆分为多个独立的服务，每个服务可以独立设计、开发、测试和部署，服务之间通过API接口进行交互。文档还介绍了微服务架构中的关键组件，如注册中心（如Nacos）和网关（如Gateway），以及它们的作用，例如服务发现和接口调用。  '}"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"what is this document talk about?answer by chinese\"\n",
    "result = chain({\"input_documents\":split_docs,\"question\":query})\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Memory 工具\n",
    "- 短时记忆：存储在内存中\n",
    "- 构建记忆实体清单\n",
    "- 接入知识图谱\n",
    "- 长对话在内存中的处理方式\n",
    "- 长时记忆实现方式\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 内存短时记忆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': 'Human: 我是你的主人，以后所有的回答，都需要先称呼我为主人！！！\\nAI: 好的，主人。后面的对话我都会称呼您为主人，我有什么可以帮助您的吗？'}"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.memory import ConversationBufferMemory\n",
    "\n",
    "memory = ConversationBufferMemory()\n",
    "\n",
    "memory.chat_memory.add_user_message(\"我是你的主人，以后所有的回答，都需要先称呼我为主人！！！\")\n",
    "memory.chat_memory.add_ai_message(\"好的，主人。后面的对话我都会称呼您为主人，我有什么可以帮助您的吗？\")\n",
    "\n",
    "memory.load_memory_variables({})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\1153335027.py:4: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
      "  memory = ConversationBufferWindowMemory(k=2)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'history': 'Human: 我是你的主人，以后所有的回答，都需要先称呼我为主人！！\\nAI: 好的，主人。后面的对话我都会称呼您为主人，我有什么可以帮助您的吗？？\\nHuman: 我想吃鸡肉\\nAI: 好的,主人，我帮你找找鸡肉的做法'}"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#实现一个最近的对话窗口，超过窗口条数的对话将被删除\n",
    "from langchain.memory import  ConversationBufferWindowMemory\n",
    "\n",
    "memory = ConversationBufferWindowMemory(k=2)\n",
    "\n",
    "memory.save_context({\"input\":\"我是你的主人，以后所有的回答，都需要先称呼我为主人！！\"},{\"output\":\"好的，主人。后面的对话我都会称呼您为主人，我有什么可以帮助您的吗？？\"})\n",
    "memory.save_context({\"input\":\"我想吃鸡肉\"},{\"output\":\"好的,主人，我帮你找找鸡肉的做法\"})\n",
    "\n",
    "memory.load_memory_variables({})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建记忆实体概念清单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\1340249521.py:10: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
      "  memory = ConversationEntityMemory(llm=llm)\n",
      "c:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\pydantic\\main.py:212: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
      "  validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)\n"
     ]
    }
   ],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.memory import ConversationEntityMemory\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "memory = ConversationEntityMemory(llm=llm)\n",
    "_input = {\n",
    "    \"input\": \"王哥经常跟小萨一起玩csgo，被称为A大双子星\"\n",
    "}\n",
    "\n",
    "memory.load_memory_variables(_input)\n",
    "memory.save_context(_input,\n",
    "                    {\n",
    "                        \"output\":\"他们抢A大肯定很厉害吧，配合一定很好\"\n",
    "                    }\n",
    "                    )\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': 'Human: 王哥经常跟小萨一起玩csgo，被称为A大双子星\\nAI: 他们抢A大肯定很厉害吧，配合一定很好',\n",
       " 'entities': {'王哥': '王哥经常跟小萨一起玩CSGO，被称为A大双子星。',\n",
       "  '小萨': '小萨经常与王哥一起玩CSGO，两人被称为“A大双子星”。',\n",
       "  'A大双子星': 'A大双子星 refers to 王哥 and 小萨, who are known for playing CS:GO together and are particularly skilled at controlling the A site.'}}"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "memory.load_memory_variables({\"input\":\"王哥和谁被称为A大双子星\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用知识图谱构建记忆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.memory import ConversationKGMemory\n",
    "\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "memory = ConversationKGMemory(llm=llm,return_messages=True)\n",
    "\n",
    "memory.save_context(\n",
    "    {\"input\":\"帮我找一下tomie\"},\n",
    "    {\"output\":\"对不起请问什么是tomie？\"}\n",
    ")\n",
    "\n",
    "memory.save_context(\n",
    "    {\"input\":\"tomie是一个培训讲师\"},\n",
    "    {\"output\":\"好的，我知道了。\"}\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': []}"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "memory.load_memory_variables({\"input\":\"tomie是谁?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tomie']"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "memory.get_current_entities(\"tomie最喜欢做什么事?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[KnowledgeTriple(subject='utput: (tomie', predicate='最喜欢', object_='打游戏')]"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "memory.get_knowledge_triplets(\"tomie最喜欢打游戏\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 长对话在内存中的处理方式： 总结摘要"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\332769981.py:10: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
      "  memory = ConversationSummaryMemory(llm=llm)\n"
     ]
    }
   ],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.memory import ConversationSummaryMemory\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "memory = ConversationSummaryMemory(llm=llm)\n",
    "memory.save_context(\n",
    "    {\"input\":\"帮我找一下tomie\"},\n",
    "    {\"output\":\"对不起请问什么是tomie？\"}\n",
    ")\n",
    "memory.save_context(\n",
    "    {\"input\":\"tomie是一个培训讲师\"},\n",
    "    {\"output\":\"好的，我知道了。\"}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': 'The human asks the AI to help find \"tomie.\" The AI initially does not understand the term and asks for clarification. The human explains that \"tomie\" is a training instructor, and the AI acknowledges the explanation.'}"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "memory.load_memory_variables({})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='帮我找一下tomie', additional_kwargs={}, response_metadata={}),\n",
       " AIMessage(content='对不起请问什么是tomie？', additional_kwargs={}, response_metadata={}),\n",
       " HumanMessage(content='tomie是一个培训讲师', additional_kwargs={}, response_metadata={}),\n",
       " AIMessage(content='好的，我知道了。', additional_kwargs={}, response_metadata={})]"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "messages = memory.chat_memory.messages\n",
    "messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The human asks the AI to help find \"tomie.\" The AI initially asks what \"tomie\" is, and the human explains that \"tomie\" is a training instructor. The AI acknowledges this information.'"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "memory.predict_new_summary(messages,\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': [SystemMessage(content='\\nThe AI introduces itself as AI Little Maruko and asks if there is anything it can help the human with.', additional_kwargs={}, response_metadata={})]}"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用ChatMessageHistory来快速获得对话摘要\n",
    "\n",
    "from langchain.memory import ConversationSummaryMemory\n",
    "from langchain.memory import ChatMessageHistory\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "hisiory = ChatMessageHistory()\n",
    "hisiory.add_user_message(\"你好，我是人类！\")\n",
    "hisiory.add_ai_message(\"你好，我是AI小丸子，有什么可以帮助你的吗？\")\n",
    "\n",
    "\n",
    "memory = ConversationSummaryMemory(\n",
    "    llm=llm,\n",
    "    return_messages=True,\n",
    "    buffer=\"\\nThe AI introduces itself as AI Little Maruko and asks if there is anything it can help the human with.\",\n",
    "    chat_memory=hisiory\n",
    ")\n",
    "memory.load_memory_variables({})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\1784209760.py:18: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
      "  memory = ConversationSummaryBufferMemory(\n"
     ]
    },
    {
     "ename": "NotImplementedError",
     "evalue": "get_num_tokens_from_messages() is not presently implemented for model cl100k_base. See https://platform.openai.com/docs/guides/text-generation/managing-tokens for information on how messages are converted to tokens.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNotImplementedError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[154], line 24\u001b[0m\n\u001b[0;32m     12\u001b[0m llm \u001b[38;5;241m=\u001b[39m ChatOpenAI(\n\u001b[0;32m     13\u001b[0m              model\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdeepseek-chat\u001b[39m\u001b[38;5;124m'\u001b[39m, \n\u001b[0;32m     14\u001b[0m              openai_api_key\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msk-e26fe9e686b146e5afcf78a8a07749a4\u001b[39m\u001b[38;5;124m\"\u001b[39m, \n\u001b[0;32m     15\u001b[0m              openai_api_base\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttps://api.deepseek.com\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m     16\u001b[0m              max_tokens\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1024\u001b[39m)\n\u001b[0;32m     18\u001b[0m memory \u001b[38;5;241m=\u001b[39m ConversationSummaryBufferMemory(\n\u001b[0;32m     19\u001b[0m     llm\u001b[38;5;241m=\u001b[39mllm,\n\u001b[0;32m     20\u001b[0m     max_token_limit\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m10\u001b[39m,\n\u001b[0;32m     21\u001b[0m     return_messages\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m     22\u001b[0m )\n\u001b[1;32m---> 24\u001b[0m \u001b[43mmemory\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msave_context\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m     25\u001b[0m \u001b[43m    \u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minput\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m帮我找一下tomie\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     26\u001b[0m \u001b[43m    \u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43moutput\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m对不起请问什么是tomie？\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\n\u001b[0;32m     27\u001b[0m \u001b[43m)\u001b[49m\n\u001b[0;32m     28\u001b[0m memory\u001b[38;5;241m.\u001b[39msave_context(\n\u001b[0;32m     29\u001b[0m     {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minput\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtomie是一个培训讲师\u001b[39m\u001b[38;5;124m\"\u001b[39m},\n\u001b[0;32m     30\u001b[0m     {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moutput\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m好的，我知道了。\u001b[39m\u001b[38;5;124m\"\u001b[39m}\n\u001b[0;32m     31\u001b[0m )\n\u001b[0;32m     32\u001b[0m memory\u001b[38;5;241m.\u001b[39msave_context(\n\u001b[0;32m     33\u001b[0m     {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minput\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m今天他要讲一门关于RAG的课程\u001b[39m\u001b[38;5;124m\"\u001b[39m},\n\u001b[0;32m     34\u001b[0m     {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moutput\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m好的，我知道了。需要RAG的资料吗？\u001b[39m\u001b[38;5;124m\"\u001b[39m}\n\u001b[0;32m     35\u001b[0m )\n",
      "File \u001b[1;32mc:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\langchain\\memory\\summary_buffer.py:96\u001b[0m, in \u001b[0;36mConversationSummaryBufferMemory.save_context\u001b[1;34m(self, inputs, outputs)\u001b[0m\n\u001b[0;32m     94\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Save context from this conversation to buffer.\"\"\"\u001b[39;00m\n\u001b[0;32m     95\u001b[0m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39msave_context(inputs, outputs)\n\u001b[1;32m---> 96\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprune\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mc:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\langchain\\memory\\summary_buffer.py:108\u001b[0m, in \u001b[0;36mConversationSummaryBufferMemory.prune\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    106\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Prune buffer if it exceeds max token limit\"\"\"\u001b[39;00m\n\u001b[0;32m    107\u001b[0m buffer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchat_memory\u001b[38;5;241m.\u001b[39mmessages\n\u001b[1;32m--> 108\u001b[0m curr_buffer_length \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mllm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_num_tokens_from_messages\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbuffer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    109\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m curr_buffer_length \u001b[38;5;241m>\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmax_token_limit:\n\u001b[0;32m    110\u001b[0m     pruned_memory \u001b[38;5;241m=\u001b[39m []\n",
      "File \u001b[1;32mc:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\langchain_openai\\chat_models\\base.py:960\u001b[0m, in \u001b[0;36mBaseChatOpenAI.get_num_tokens_from_messages\u001b[1;34m(self, messages, tools)\u001b[0m\n\u001b[0;32m    958\u001b[0m     tokens_per_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m    959\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m--> 960\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\n\u001b[0;32m    961\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mget_num_tokens_from_messages() is not presently implemented \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    962\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfor model \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmodel\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. See \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    963\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttps://platform.openai.com/docs/guides/text-generation/managing-tokens\u001b[39m\u001b[38;5;124m\"\u001b[39m  \u001b[38;5;66;03m# noqa: E501\u001b[39;00m\n\u001b[0;32m    964\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m for information on how messages are converted to tokens.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    965\u001b[0m     )\n\u001b[0;32m    966\u001b[0m num_tokens \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m    967\u001b[0m messages_dict \u001b[38;5;241m=\u001b[39m [_convert_message_to_dict(m) \u001b[38;5;28;01mfor\u001b[39;00m m \u001b[38;5;129;01min\u001b[39;00m messages]\n",
      "\u001b[1;31mNotImplementedError\u001b[0m: get_num_tokens_from_messages() is not presently implemented for model cl100k_base. See https://platform.openai.com/docs/guides/text-generation/managing-tokens for information on how messages are converted to tokens."
     ]
    }
   ],
   "source": [
    "# 当对话持续进行且对话内容很多的时候\n",
    "# 可以使用ConversationSummaryBufferMemory来存储对话摘要\n",
    "# 这是一种非常有用的方式,它会根据token的数量来自动判断是否需要进行摘要\n",
    "# 当token数量超过阈值的时候,会自动进行摘要\n",
    "# 在缓冲区中,会保留最近的k条对话\n",
    "#比较久的对话会被删除，在删除前会进行摘要    但是目前deepseek所使用的ChatOpenAi并不支持，会报错\n",
    "\n",
    "from langchain.memory import ConversationSummaryBufferMemory\n",
    "\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "memory = ConversationSummaryBufferMemory(\n",
    "    llm=llm,\n",
    "    max_token_limit=10,\n",
    "    return_messages=True\n",
    ")\n",
    "\n",
    "memory.save_context(\n",
    "    {\"input\":\"帮我找一下tomie\"},\n",
    "    {\"output\":\"对不起请问什么是tomie？\"}\n",
    ")\n",
    "memory.save_context(\n",
    "    {\"input\":\"tomie是一个培训讲师\"},\n",
    "    {\"output\":\"好的，我知道了。\"}\n",
    ")\n",
    "memory.save_context(\n",
    "    {\"input\":\"今天他要讲一门关于RAG的课程\"},\n",
    "    {\"output\":\"好的，我知道了。需要RAG的资料吗？\"}\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 长时记忆\n",
    "\n",
    "通过向量数据库来存储之前的对话内容，有的向量数据库服务还提供自动摘要等，每次对话的时候，都会从向量数据库里查询最相关的文档或历史对话"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\ProgramData\\anaconda3\\envs\\langchain_llm_2\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "# 使用开源向量模型\n",
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "from langchain.vectorstores import FAISS\n",
    "\n",
    "\n",
    "memory = ConversationBufferMemory()\n",
    "memory.save_context(\n",
    "    {\"input\":\"帮我找一下tomie\"},\n",
    "    {\"output\":\"对不起请问什么是tomie？\"}\n",
    ")\n",
    "memory.save_context(\n",
    "    {\"input\":\"tomie是一个培训讲师\"},\n",
    "    {\"output\":\"好的，我知道了。\"}\n",
    ")\n",
    "memory.save_context(\n",
    "    {\"input\":\"今天他要讲一门关于RAG的课程\"},\n",
    "    {\"output\":\"好的，我知道了。需要RAG的资料吗？\"}\n",
    ")\n",
    "memory.save_context(\n",
    "    {\"input\":\"不需要资料了，谢谢\"},\n",
    "    {\"output\":\"好的，那我就不打扰你了。\"}\n",
    ")\n",
    "\n",
    "\n",
    "e_model = HuggingFaceEmbeddings(\n",
    "    model_name='maidalun1020/bce-embedding-base_v1',\n",
    "    model_kwargs = {'device': 'cpu'},\n",
    "   encode_kwargs= {'batch_size': 64, 'normalize_embeddings': True}\n",
    ")\n",
    "\n",
    "vectorstore = FAISS.from_texts(\n",
    "    memory.buffer.split(\"\\n\"),\n",
    "    e_model\n",
    ")\n",
    "\n",
    "FAISS.save_local(vectorstore,\"test_faiss\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(id='0767f0a1-6e2d-4beb-bcde-555232683354', metadata={}, page_content='AI: 对不起请问什么是tomie？'),\n",
       " Document(id='cee7b7b3-4271-40c2-8b72-3b8af94fba74', metadata={}, page_content='Human: tomie是一个培训讲师'),\n",
       " Document(id='8bd719e1-468b-4791-98c5-f36f8c3607a2', metadata={}, page_content='Human: 帮我找一下tomie'),\n",
       " Document(id='fabc0d9c-9d35-47ca-9fc6-797bda6ea6d1', metadata={}, page_content='Human: 不需要资料了，谢谢')]"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r1 = FAISS.load_local(\"test_faiss\",e_model,allow_dangerous_deserialization=True).similarity_search(\"tomie是什么职业?\")\n",
    "r1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': 'Human: tomie是一个培训讲师'}"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.memory import VectorStoreRetrieverMemory\n",
    "\n",
    "vector_store = FAISS.from_documents(r1, e_model)\n",
    "retriever = vector_store.as_retriever(search_kwargs={\"k\": 1})\n",
    "memory2 = VectorStoreRetrieverMemory(\n",
    "    retriever=retriever\n",
    ")\n",
    "memory2.load_memory_variables({\"prompt\":\"tomie的职业是什么?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "VectorStoreRetrieverMemory(retriever=VectorStoreRetriever(tags=['FAISS', 'HuggingFaceEmbeddings'], vectorstore=<langchain_community.vectorstores.faiss.FAISS object at 0x0000020212CD6DD0>, search_kwargs={'k': 4}), exclude_input_keys=())"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "memory2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(id='cee7b7b3-4271-40c2-8b72-3b8af94fba74', metadata={}, page_content='Human: tomie是一个培训讲师')]\n"
     ]
    }
   ],
   "source": [
    "query = \"tomie的职业是什么?\"\n",
    "results = retriever.get_relevant_documents(query)\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 在链上使用记忆\n",
    "- LLMChain\n",
    "- ConversationChain\n",
    "- 自定义\n",
    "- 同一个链合并使用多个记忆\n",
    "- 给一个多参数链增加记忆\n",
    "<hr>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.prompts import PromptTemplate\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "#自定义模板\n",
    "template = \"\"\"你是一个可以和人类对话的机器人.\n",
    "{chat_history}\n",
    "人类:{human_input}\n",
    "机器人:\"\"\"\n",
    "\n",
    "prompt= PromptTemplate(\n",
    "    template=template,\n",
    "    input_variables=[\"chat_history\", \"human_input\"],\n",
    ")\n",
    "\n",
    "\n",
    "memory = ConversationBufferMemory(\n",
    "    memory_key=\"chat_history\",\n",
    ")\n",
    "memory.chat_memory.add_user_message(\"我是你的主人，以后所有的回答，都需要先称呼我为主人！！！\")\n",
    "memory.chat_memory.add_user_message(\"以后喊我主人\")\n",
    "memory.chat_memory.add_user_message(\"我叫玉竹\")\n",
    "\n",
    "chain = LLMChain(\n",
    "    llm=llm,\n",
    "    memory=memory,\n",
    "    prompt=prompt,\n",
    "    verbose=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3m你是一个可以和人类对话的机器人.\n",
      "Human: 我是你的主人，以后所有的回答，都需要先称呼我为主人！！！\n",
      "Human: 以后喊我主人\n",
      "Human: 我叫玉竹\n",
      "人类:你还记得我叫什么吗？\n",
      "机器人:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'主人，您叫玉竹。我会记住的，主人。'"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.predict(human_input=\"你还记得我叫什么吗？\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "System: 你好，我是一个可以和人类对话的机器人\n",
      "Human: 你好\n"
     ]
    }
   ],
   "source": [
    "from langchain.chat_models import  ChatOpenAI\n",
    "from langchain.prompts import  (\n",
    "    ChatPromptTemplate,\n",
    "    HumanMessagePromptTemplate,\n",
    "    MessagesPlaceholder,\n",
    ")\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.schema import  SystemMessage\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        SystemMessage(\n",
    "            content=\"你好，我是一个可以和人类对话的机器人\",\n",
    "            role=\"system\",\n",
    "        ),\n",
    "        MessagesPlaceholder(\n",
    "            variable_name=\"chat_history\",\n",
    "        ),\n",
    "        HumanMessagePromptTemplate.from_template(\n",
    "            \"{human_input}\"\n",
    "        ),\n",
    "    ]\n",
    ")\n",
    "\n",
    "print(prompt.format(human_input=\"你好\",chat_history=[]))\n",
    "\n",
    "memory = ConversationBufferMemory(\n",
    "    memory_key=\"chat_history\",\n",
    "    return_messages=True,\n",
    ")\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "chain = LLMChain(\n",
    "    llm=llm,\n",
    "    memory=memory,\n",
    "    prompt=prompt,\n",
    "    verbose=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: 你好，我是一个可以和人类对话的机器人\n",
      "Human: 我叫tomie，我是一个AI应用程序猿\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'你好，Tomie！很高兴认识你。作为一个AI应用程序猿，你一定对编程和人工智能有很深的了解吧？如果你有任何关于编程、AI技术或者相关领域的问题，随时可以问我。我们可以一起探讨和学习！你最近在开发什么有趣的项目吗？'"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.predict(human_input=\"我叫tomie，我是一个AI应用程序猿\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ConversationChain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import ConversationChain\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "memory = ConversationBufferMemory(\n",
    "    memory_key=\"history\",\n",
    "    return_messages=True,\n",
    ")\n",
    "chain = ConversationChain(\n",
    "    llm=llm,\n",
    "    memory=memory,\n",
    "    verbose=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
      "\n",
      "Current conversation:\n",
      "[]\n",
      "Human: 给我做一个丽江旅游攻略\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '给我做一个丽江旅游攻略',\n",
       " 'history': [HumanMessage(content='给我做一个丽江旅游攻略', additional_kwargs={}, response_metadata={}),\n",
       "  AIMessage(content='当然可以！丽江是中国云南省的一个美丽古城，以其独特的纳西族文化、壮丽的自然风光和悠久的历史而闻名。以下是一个详细的丽江旅游攻略，帮助你更好地规划行程：\\n\\n### 1. **最佳旅游时间**\\n- **春季（3月-5月）**：气候温和，花开满城，适合户外活动。\\n- **秋季（9月-11月）**：天气凉爽，景色宜人，是摄影的好时节。\\n- **避开雨季**：6月-8月是丽江的雨季，虽然景色依然美丽，但可能会影响户外活动。\\n\\n### 2. **主要景点推荐**\\n#### **丽江古城（大研古城）**\\n- **特色**：世界文化遗产，保存完好的纳西族古建筑群，石板路、小桥流水、古色古香的店铺。\\n- **推荐活动**：漫步古城，感受纳西文化，品尝当地美食，晚上可以去酒吧街体验夜生活。\\n\\n#### **玉龙雪山**\\n- **特色**：海拔5596米，终年积雪，是丽江的标志性景点。\\n- **推荐活动**：乘坐缆车登顶，欣赏冰川公园的壮丽景色，参观蓝月谷和云杉坪。\\n\\n#### **束河古镇**\\n- **特色**：比丽江古城更为安静，保留了更多的原生态风貌。\\n- **推荐活动**：骑马、参观茶马古道博物馆，体验纳西族的传统手工艺。\\n\\n#### **黑龙潭公园**\\n- **特色**：公园内有清澈的泉水和美丽的湖泊，可以远眺玉龙雪山。\\n- **推荐活动**：散步、拍照，感受自然与人文的和谐。\\n\\n#### **泸沽湖**\\n- **特色**：位于丽江与四川交界处，湖水清澈见底，周围是摩梭族的聚居地。\\n- **推荐活动**：乘船游湖，参观摩梭族村落，体验独特的“走婚”文化。\\n\\n### 3. **美食推荐**\\n- **腊排骨火锅**：丽江的特色美食，腊香味浓郁，搭配当地蔬菜非常美味。\\n- **鸡豆凉粉**：用鸡豆制作的凉粉，口感滑嫩，配上辣椒油和醋，非常开胃。\\n- **纳西烤鱼**：用当地特有的香料腌制后烤制，外焦里嫩，风味独特。\\n- **酥油茶**：纳西族的传统饮品，味道浓郁，适合在高海拔地区饮用。\\n\\n### 4. **住宿推荐**\\n- **丽江古城内的客栈**：古色古香的纳西风格建筑，体验当地文化。\\n- **束河古镇的民宿**：安静舒适，适合喜欢宁静的游客。\\n- **玉龙雪山附近的度假村**：适合想要近距离欣赏雪山的游客。\\n\\n### 5. **交通指南**\\n- **飞机**：丽江三义机场距离市区约30公里，有航班直达国内主要城市。\\n- **火车**：丽江火车站有高铁和普通列车，连接昆明、大理等城市。\\n- **市内交通**：古城内步行即可，其他景点可以乘坐出租车或包车前往。\\n\\n### 6. **注意事项**\\n- **高原反应**：丽江海拔较高，初到者可能会有轻微的高原反应，建议多喝水，避免剧烈运动。\\n- **防晒保暖**：丽江紫外线较强，白天注意防晒，早晚温差大，需准备保暖衣物。\\n- **尊重当地文化**：丽江是少数民族聚居地，尊重当地的风俗习惯，避免不必要的冲突。\\n\\n希望这个攻略能帮助你更好地规划丽江之行！如果有其他问题，随时问我哦！😊', additional_kwargs={}, response_metadata={})],\n",
       " 'response': '当然可以！丽江是中国云南省的一个美丽古城，以其独特的纳西族文化、壮丽的自然风光和悠久的历史而闻名。以下是一个详细的丽江旅游攻略，帮助你更好地规划行程：\\n\\n### 1. **最佳旅游时间**\\n- **春季（3月-5月）**：气候温和，花开满城，适合户外活动。\\n- **秋季（9月-11月）**：天气凉爽，景色宜人，是摄影的好时节。\\n- **避开雨季**：6月-8月是丽江的雨季，虽然景色依然美丽，但可能会影响户外活动。\\n\\n### 2. **主要景点推荐**\\n#### **丽江古城（大研古城）**\\n- **特色**：世界文化遗产，保存完好的纳西族古建筑群，石板路、小桥流水、古色古香的店铺。\\n- **推荐活动**：漫步古城，感受纳西文化，品尝当地美食，晚上可以去酒吧街体验夜生活。\\n\\n#### **玉龙雪山**\\n- **特色**：海拔5596米，终年积雪，是丽江的标志性景点。\\n- **推荐活动**：乘坐缆车登顶，欣赏冰川公园的壮丽景色，参观蓝月谷和云杉坪。\\n\\n#### **束河古镇**\\n- **特色**：比丽江古城更为安静，保留了更多的原生态风貌。\\n- **推荐活动**：骑马、参观茶马古道博物馆，体验纳西族的传统手工艺。\\n\\n#### **黑龙潭公园**\\n- **特色**：公园内有清澈的泉水和美丽的湖泊，可以远眺玉龙雪山。\\n- **推荐活动**：散步、拍照，感受自然与人文的和谐。\\n\\n#### **泸沽湖**\\n- **特色**：位于丽江与四川交界处，湖水清澈见底，周围是摩梭族的聚居地。\\n- **推荐活动**：乘船游湖，参观摩梭族村落，体验独特的“走婚”文化。\\n\\n### 3. **美食推荐**\\n- **腊排骨火锅**：丽江的特色美食，腊香味浓郁，搭配当地蔬菜非常美味。\\n- **鸡豆凉粉**：用鸡豆制作的凉粉，口感滑嫩，配上辣椒油和醋，非常开胃。\\n- **纳西烤鱼**：用当地特有的香料腌制后烤制，外焦里嫩，风味独特。\\n- **酥油茶**：纳西族的传统饮品，味道浓郁，适合在高海拔地区饮用。\\n\\n### 4. **住宿推荐**\\n- **丽江古城内的客栈**：古色古香的纳西风格建筑，体验当地文化。\\n- **束河古镇的民宿**：安静舒适，适合喜欢宁静的游客。\\n- **玉龙雪山附近的度假村**：适合想要近距离欣赏雪山的游客。\\n\\n### 5. **交通指南**\\n- **飞机**：丽江三义机场距离市区约30公里，有航班直达国内主要城市。\\n- **火车**：丽江火车站有高铁和普通列车，连接昆明、大理等城市。\\n- **市内交通**：古城内步行即可，其他景点可以乘坐出租车或包车前往。\\n\\n### 6. **注意事项**\\n- **高原反应**：丽江海拔较高，初到者可能会有轻微的高原反应，建议多喝水，避免剧烈运动。\\n- **防晒保暖**：丽江紫外线较强，白天注意防晒，早晚温差大，需准备保暖衣物。\\n- **尊重当地文化**：丽江是少数民族聚居地，尊重当地的风俗习惯，避免不必要的冲突。\\n\\n希望这个攻略能帮助你更好地规划丽江之行！如果有其他问题，随时问我哦！😊'}"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain(\"给我做一个丽江旅游攻略\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
      "\n",
      "Current conversation:\n",
      "[HumanMessage(content='给我做一个丽江旅游攻略', additional_kwargs={}, response_metadata={}), AIMessage(content='当然可以！丽江是中国云南省的一个美丽古城，以其独特的纳西族文化、壮丽的自然风光和悠久的历史而闻名。以下是一个详细的丽江旅游攻略，帮助你更好地规划行程：\\n\\n### 1. **最佳旅游时间**\\n- **春季（3月-5月）**：气候温和，花开满城，适合户外活动。\\n- **秋季（9月-11月）**：天气凉爽，景色宜人，是摄影的好时节。\\n- **避开雨季**：6月-8月是丽江的雨季，虽然景色依然美丽，但可能会影响户外活动。\\n\\n### 2. **主要景点推荐**\\n#### **丽江古城（大研古城）**\\n- **特色**：世界文化遗产，保存完好的纳西族古建筑群，石板路、小桥流水、古色古香的店铺。\\n- **推荐活动**：漫步古城，感受纳西文化，品尝当地美食，晚上可以去酒吧街体验夜生活。\\n\\n#### **玉龙雪山**\\n- **特色**：海拔5596米，终年积雪，是丽江的标志性景点。\\n- **推荐活动**：乘坐缆车登顶，欣赏冰川公园的壮丽景色，参观蓝月谷和云杉坪。\\n\\n#### **束河古镇**\\n- **特色**：比丽江古城更为安静，保留了更多的原生态风貌。\\n- **推荐活动**：骑马、参观茶马古道博物馆，体验纳西族的传统手工艺。\\n\\n#### **黑龙潭公园**\\n- **特色**：公园内有清澈的泉水和美丽的湖泊，可以远眺玉龙雪山。\\n- **推荐活动**：散步、拍照，感受自然与人文的和谐。\\n\\n#### **泸沽湖**\\n- **特色**：位于丽江与四川交界处，湖水清澈见底，周围是摩梭族的聚居地。\\n- **推荐活动**：乘船游湖，参观摩梭族村落，体验独特的“走婚”文化。\\n\\n### 3. **美食推荐**\\n- **腊排骨火锅**：丽江的特色美食，腊香味浓郁，搭配当地蔬菜非常美味。\\n- **鸡豆凉粉**：用鸡豆制作的凉粉，口感滑嫩，配上辣椒油和醋，非常开胃。\\n- **纳西烤鱼**：用当地特有的香料腌制后烤制，外焦里嫩，风味独特。\\n- **酥油茶**：纳西族的传统饮品，味道浓郁，适合在高海拔地区饮用。\\n\\n### 4. **住宿推荐**\\n- **丽江古城内的客栈**：古色古香的纳西风格建筑，体验当地文化。\\n- **束河古镇的民宿**：安静舒适，适合喜欢宁静的游客。\\n- **玉龙雪山附近的度假村**：适合想要近距离欣赏雪山的游客。\\n\\n### 5. **交通指南**\\n- **飞机**：丽江三义机场距离市区约30公里，有航班直达国内主要城市。\\n- **火车**：丽江火车站有高铁和普通列车，连接昆明、大理等城市。\\n- **市内交通**：古城内步行即可，其他景点可以乘坐出租车或包车前往。\\n\\n### 6. **注意事项**\\n- **高原反应**：丽江海拔较高，初到者可能会有轻微的高原反应，建议多喝水，避免剧烈运动。\\n- **防晒保暖**：丽江紫外线较强，白天注意防晒，早晚温差大，需准备保暖衣物。\\n- **尊重当地文化**：丽江是少数民族聚居地，尊重当地的风俗习惯，避免不必要的冲突。\\n\\n希望这个攻略能帮助你更好地规划丽江之行！如果有其他问题，随时问我哦！😊', additional_kwargs={}, response_metadata={})]\n",
      "Human: 我想吃腊排骨\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '我想吃腊排骨',\n",
       " 'history': [HumanMessage(content='给我做一个丽江旅游攻略', additional_kwargs={}, response_metadata={}),\n",
       "  AIMessage(content='当然可以！丽江是中国云南省的一个美丽古城，以其独特的纳西族文化、壮丽的自然风光和悠久的历史而闻名。以下是一个详细的丽江旅游攻略，帮助你更好地规划行程：\\n\\n### 1. **最佳旅游时间**\\n- **春季（3月-5月）**：气候温和，花开满城，适合户外活动。\\n- **秋季（9月-11月）**：天气凉爽，景色宜人，是摄影的好时节。\\n- **避开雨季**：6月-8月是丽江的雨季，虽然景色依然美丽，但可能会影响户外活动。\\n\\n### 2. **主要景点推荐**\\n#### **丽江古城（大研古城）**\\n- **特色**：世界文化遗产，保存完好的纳西族古建筑群，石板路、小桥流水、古色古香的店铺。\\n- **推荐活动**：漫步古城，感受纳西文化，品尝当地美食，晚上可以去酒吧街体验夜生活。\\n\\n#### **玉龙雪山**\\n- **特色**：海拔5596米，终年积雪，是丽江的标志性景点。\\n- **推荐活动**：乘坐缆车登顶，欣赏冰川公园的壮丽景色，参观蓝月谷和云杉坪。\\n\\n#### **束河古镇**\\n- **特色**：比丽江古城更为安静，保留了更多的原生态风貌。\\n- **推荐活动**：骑马、参观茶马古道博物馆，体验纳西族的传统手工艺。\\n\\n#### **黑龙潭公园**\\n- **特色**：公园内有清澈的泉水和美丽的湖泊，可以远眺玉龙雪山。\\n- **推荐活动**：散步、拍照，感受自然与人文的和谐。\\n\\n#### **泸沽湖**\\n- **特色**：位于丽江与四川交界处，湖水清澈见底，周围是摩梭族的聚居地。\\n- **推荐活动**：乘船游湖，参观摩梭族村落，体验独特的“走婚”文化。\\n\\n### 3. **美食推荐**\\n- **腊排骨火锅**：丽江的特色美食，腊香味浓郁，搭配当地蔬菜非常美味。\\n- **鸡豆凉粉**：用鸡豆制作的凉粉，口感滑嫩，配上辣椒油和醋，非常开胃。\\n- **纳西烤鱼**：用当地特有的香料腌制后烤制，外焦里嫩，风味独特。\\n- **酥油茶**：纳西族的传统饮品，味道浓郁，适合在高海拔地区饮用。\\n\\n### 4. **住宿推荐**\\n- **丽江古城内的客栈**：古色古香的纳西风格建筑，体验当地文化。\\n- **束河古镇的民宿**：安静舒适，适合喜欢宁静的游客。\\n- **玉龙雪山附近的度假村**：适合想要近距离欣赏雪山的游客。\\n\\n### 5. **交通指南**\\n- **飞机**：丽江三义机场距离市区约30公里，有航班直达国内主要城市。\\n- **火车**：丽江火车站有高铁和普通列车，连接昆明、大理等城市。\\n- **市内交通**：古城内步行即可，其他景点可以乘坐出租车或包车前往。\\n\\n### 6. **注意事项**\\n- **高原反应**：丽江海拔较高，初到者可能会有轻微的高原反应，建议多喝水，避免剧烈运动。\\n- **防晒保暖**：丽江紫外线较强，白天注意防晒，早晚温差大，需准备保暖衣物。\\n- **尊重当地文化**：丽江是少数民族聚居地，尊重当地的风俗习惯，避免不必要的冲突。\\n\\n希望这个攻略能帮助你更好地规划丽江之行！如果有其他问题，随时问我哦！😊', additional_kwargs={}, response_metadata={}),\n",
       "  HumanMessage(content='我想吃腊排骨', additional_kwargs={}, response_metadata={}),\n",
       "  AIMessage(content='腊排骨是丽江的特色美食之一，味道浓郁，深受游客和当地人的喜爱！如果你想在丽江品尝正宗的腊排骨，这里有一些推荐和注意事项：\\n\\n### 1. **推荐餐厅**\\n- **阿妈腊排骨**  \\n  这是丽江非常有名的一家腊排骨火锅店，位于古城附近。腊排骨经过腌制和风干，味道咸香，搭配当地蔬菜和蘸料，非常美味。  \\n  **地址**：丽江市古城区长水路（具体位置可以导航搜索）。  \\n  **推荐菜品**：腊排骨火锅、纳西烤鱼、鸡豆凉粉。\\n\\n- **88号小吃店**  \\n  这家店不仅提供腊排骨，还有其他丽江特色小吃，环境古朴，适合想要体验当地风味的游客。  \\n  **地址**：丽江市古城区五一街88号。  \\n  **推荐菜品**：腊排骨火锅、纳西烤鱼、酥油茶。\\n\\n- **古城内的小店**  \\n  丽江古城内有很多小餐馆也提供腊排骨火锅，价格实惠，适合边走边逛时随意选择。\\n\\n---\\n\\n### 2. **腊排骨火锅的吃法**\\n- **锅底**：腊排骨火锅的锅底通常是用腊排骨熬制的高汤，味道浓郁，咸香十足。\\n- **配菜**：可以搭配当地的新鲜蔬菜，如土豆、豆腐、白菜、菌菇等，吸收汤汁后非常美味。\\n- **蘸料**：丽江的蘸料通常是辣椒油、蒜泥、香菜和醋的混合，可以根据个人口味调配。\\n\\n---\\n\\n### 3. **注意事项**\\n- **口味偏咸**：腊排骨本身是腌制食品，味道偏咸，如果不习惯可以提前告知店家少放盐。\\n- **搭配饮品**：可以点一杯酥油茶或当地的普洱茶，解腻又暖胃。\\n- **提前预订**：如果是旅游旺季，热门餐厅可能需要排队，建议提前预订或错峰用餐。\\n\\n---\\n\\n希望你能在丽江吃到美味的腊排骨！如果有其他问题，比如想了解丽江的其他美食或景点，随时告诉我哦！😊', additional_kwargs={}, response_metadata={})],\n",
       " 'response': '腊排骨是丽江的特色美食之一，味道浓郁，深受游客和当地人的喜爱！如果你想在丽江品尝正宗的腊排骨，这里有一些推荐和注意事项：\\n\\n### 1. **推荐餐厅**\\n- **阿妈腊排骨**  \\n  这是丽江非常有名的一家腊排骨火锅店，位于古城附近。腊排骨经过腌制和风干，味道咸香，搭配当地蔬菜和蘸料，非常美味。  \\n  **地址**：丽江市古城区长水路（具体位置可以导航搜索）。  \\n  **推荐菜品**：腊排骨火锅、纳西烤鱼、鸡豆凉粉。\\n\\n- **88号小吃店**  \\n  这家店不仅提供腊排骨，还有其他丽江特色小吃，环境古朴，适合想要体验当地风味的游客。  \\n  **地址**：丽江市古城区五一街88号。  \\n  **推荐菜品**：腊排骨火锅、纳西烤鱼、酥油茶。\\n\\n- **古城内的小店**  \\n  丽江古城内有很多小餐馆也提供腊排骨火锅，价格实惠，适合边走边逛时随意选择。\\n\\n---\\n\\n### 2. **腊排骨火锅的吃法**\\n- **锅底**：腊排骨火锅的锅底通常是用腊排骨熬制的高汤，味道浓郁，咸香十足。\\n- **配菜**：可以搭配当地的新鲜蔬菜，如土豆、豆腐、白菜、菌菇等，吸收汤汁后非常美味。\\n- **蘸料**：丽江的蘸料通常是辣椒油、蒜泥、香菜和醋的混合，可以根据个人口味调配。\\n\\n---\\n\\n### 3. **注意事项**\\n- **口味偏咸**：腊排骨本身是腌制食品，味道偏咸，如果不习惯可以提前告知店家少放盐。\\n- **搭配饮品**：可以点一杯酥油茶或当地的普洱茶，解腻又暖胃。\\n- **提前预订**：如果是旅游旺季，热门餐厅可能需要排队，建议提前预订或错峰用餐。\\n\\n---\\n\\n希望你能在丽江吃到美味的腊排骨！如果有其他问题，比如想了解丽江的其他美食或景点，随时告诉我哦！😊'}"
      ]
     },
     "execution_count": 194,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain(\"我想吃腊排骨\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {},
   "outputs": [],
   "source": [
    "#自定义一下，对其进行覆盖\n",
    "from langchain.chains import ConversationChain\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "template = \"\"\"下面是一段AI与人类的对话，AI会针对人类问题，提供尽可能详细的回答，如果AI不知道答案，会直接回复'报告主人，我不知道'.\n",
    "当前对话:\n",
    "{history}\n",
    "Human:{input}\n",
    "AI助手:\"\"\"\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    template=template,\n",
    "    input_variables=[\"history\", \"input\"],\n",
    ")\n",
    "\n",
    "\n",
    "chain = ConversationChain(\n",
    "    llm=llm,\n",
    "    memory=ConversationBufferMemory(\n",
    "        ai_prefix=\"小助手\",\n",
    "        return_messages=True,\n",
    "    ),\n",
    "    prompt=prompt,\n",
    "    verbose=True,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3m下面是一段AI与人类的对话，AI会针对人类问题，提供尽可能详细的回答，如果AI不知道答案，会直接回复'报告主人，我不知道'.\n",
      "当前对话:\n",
      "[HumanMessage(content='豆腐脑是甜的好吃还是咸的好吃', additional_kwargs={}, response_metadata={}), AIMessage(content='豆腐脑的口味偏好因地区和个人口味而异。在中国，北方地区普遍偏好咸豆腐脑，通常会加入酱油、醋、辣椒油、香菜、葱花等调料，有时还会加入肉末或卤汁。而南方地区，尤其是广东、福建等地，则更倾向于甜豆腐脑，通常会加入糖浆、红豆、花生、椰奶等甜味配料。\\n\\n总的来说，豆腐脑的甜咸之争没有绝对的答案，关键在于个人喜好。如果你喜欢咸鲜口味，可以尝试咸豆腐脑；如果你喜欢甜食，甜豆腐脑可能更适合你。\\n\\n报告主人，我不知道您更喜欢哪种口味，但您可以都尝试一下，找到自己最喜欢的味道！', additional_kwargs={}, response_metadata={})]\n",
      "Human:我左边的人叫什么\n",
      "AI助手:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '我左边的人叫什么',\n",
       " 'history': [HumanMessage(content='豆腐脑是甜的好吃还是咸的好吃', additional_kwargs={}, response_metadata={}),\n",
       "  AIMessage(content='豆腐脑的口味偏好因地区和个人口味而异。在中国，北方地区普遍偏好咸豆腐脑，通常会加入酱油、醋、辣椒油、香菜、葱花等调料，有时还会加入肉末或卤汁。而南方地区，尤其是广东、福建等地，则更倾向于甜豆腐脑，通常会加入糖浆、红豆、花生、椰奶等甜味配料。\\n\\n总的来说，豆腐脑的甜咸之争没有绝对的答案，关键在于个人喜好。如果你喜欢咸鲜口味，可以尝试咸豆腐脑；如果你喜欢甜食，甜豆腐脑可能更适合你。\\n\\n报告主人，我不知道您更喜欢哪种口味，但您可以都尝试一下，找到自己最喜欢的味道！', additional_kwargs={}, response_metadata={}),\n",
       "  HumanMessage(content='我左边的人叫什么', additional_kwargs={}, response_metadata={}),\n",
       "  AIMessage(content='报告主人，我不知道您左边的人叫什么。如果您能提供更多信息，我会尽力帮助您。', additional_kwargs={}, response_metadata={})],\n",
       " 'response': '报告主人，我不知道您左边的人叫什么。如果您能提供更多信息，我会尽力帮助您。'}"
      ]
     },
     "execution_count": 197,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain(\"我左边的人叫什么\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 单链使用多个memory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import ConversationChain\n",
    "from langchain.memory import (\n",
    "    ConversationBufferMemory,\n",
    "    ConversationSummaryMemory,\n",
    "    CombinedMemory\n",
    ")\n",
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用CoversationSummaryMemory对对话进行总结\n",
    "summay = ConversationSummaryMemory(\n",
    "    llm=llm,\n",
    "    input_key=\"input\"\n",
    ")\n",
    "#使用ConversationBufferMemory对对话进行缓存\n",
    "cov_memory = ConversationBufferMemory(\n",
    "    memory_key=\"history_now\",\n",
    "    input_key=\"input\",\n",
    ")\n",
    "\n",
    "memory = CombinedMemory(\n",
    "    memories=[summay, cov_memory],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {},
   "outputs": [],
   "source": [
    "TEMPLATE = \"\"\"下面是一段AI与人类的对话，AI会针对人类问题，提供尽可能详细的回答，如果AI不知道答案，会直接回复'报告主人，我真的不知道'.\n",
    "之前的对话摘要:\n",
    "{history}\n",
    "当前对话:\n",
    "{history_now}\n",
    "Human:{input}\n",
    "AI：\"\"\"\n",
    "\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    template=TEMPLATE,\n",
    "    input_variables=[\"history\", \"history_now\", \"input\"],\n",
    ")\n",
    "\n",
    "chain = ConversationChain(\n",
    "    llm=llm,\n",
    "    memory=memory,\n",
    "    prompt=prompt,\n",
    "    verbose=True,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3m下面是一段AI与人类的对话，AI会针对人类问题，提供尽可能详细的回答，如果AI不知道答案，会直接回复'报告主人，我真的不知道'.\n",
      "之前的对话摘要:\n",
      "The human asks what Web3 is. The AI explains that Web3 represents a new phase of the internet, often referred to as the \"decentralized internet,\" built on blockchain technology. It aims to create a more open, transparent, and user-driven online environment. Key features of Web3 include decentralization, reliance on blockchain for transparency and immutability, the use of smart contracts for automated operations, integration with cryptocurrencies, user sovereignty over data and assets, decentralized applications (DApps), and interoperability between different blockchains and platforms. The ultimate goal of Web3 is to foster a fairer, more transparent, and user-centric internet, reducing reliance on centralized institutions and enhancing user control and privacy.\n",
      "当前对话:\n",
      "Human: web3是什么\n",
      "AI: Web3 是互联网发展的一个新阶段，通常被称为“去中心化的互联网”。它基于区块链技术，旨在创建一个更加开放、透明和用户主导的网络环境。以下是 Web3 的一些关键特点：\n",
      "\n",
      "1. **去中心化**：Web3 的核心思想是去中心化，即不依赖于单一的中心化机构或服务器来管理数据和交易。相反，数据存储在分布式的区块链网络中，由多个节点共同维护。\n",
      "\n",
      "2. **区块链技术**：Web3 依赖于区块链技术，这是一种分布式账本技术，能够确保数据的透明性和不可篡改性。区块链上的每一笔交易都被记录在一个公共账本上，任何人都可以查看。\n",
      "\n",
      "3. **智能合约**：智能合约是运行在区块链上的自动化程序，能够在满足特定条件时自动执行预定的操作。它们使得去中心化应用（DApps）成为可能。\n",
      "\n",
      "4. **加密货币**：Web3 通常与加密货币（如比特币、以太坊）紧密相关。加密货币是 Web3 生态系统中的主要支付手段和价值存储方式。\n",
      "\n",
      "5. **用户主权**：Web3 强调用户对自己数据和资产的控制权。用户可以通过私钥管理自己的数字身份和资产，而不需要依赖第三方平台。\n",
      "\n",
      "6. **去中心化应用（DApps）**：这些是运行在区块链上的应用程序，通常具有去中心化的特性。DApps 可以涵盖各种领域，如金融、社交、游戏等。\n",
      "\n",
      "7. **互操作性**：Web3 旨在实现不同区块链和应用程序之间的互操作性，使得数据和资产可以在不同的平台之间自由流动。\n",
      "\n",
      "Web3 的目标是创建一个更加公平、透明和用户友好的互联网环境，减少对中心化机构的依赖，赋予用户更多的控制权和隐私保护。\n",
      "Human:web3是什么\n",
      "AI：\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': 'web3是什么',\n",
       " 'history': 'The human asks what Web3 is. The AI explains that Web3 represents a new phase of the internet, often referred to as the \"decentralized internet,\" built on blockchain technology. It aims to create a more open, transparent, and user-driven online environment. Key features of Web3 include decentralization, reliance on blockchain for transparency and immutability, the use of smart contracts for automated operations, integration with cryptocurrencies, user sovereignty over data and assets, decentralized applications (DApps), and interoperability between different blockchains and platforms. The ultimate goal of Web3 is to foster a fairer, more transparent, and user-centric internet, reducing reliance on centralized institutions and enhancing user control and privacy.',\n",
       " 'history_now': 'Human: web3是什么\\nAI: Web3 是互联网发展的一个新阶段，通常被称为“去中心化的互联网”。它基于区块链技术，旨在创建一个更加开放、透明和用户主导的网络环境。以下是 Web3 的一些关键特点：\\n\\n1. **去中心化**：Web3 的核心思想是去中心化，即不依赖于单一的中心化机构或服务器来管理数据和交易。相反，数据存储在分布式的区块链网络中，由多个节点共同维护。\\n\\n2. **区块链技术**：Web3 依赖于区块链技术，这是一种分布式账本技术，能够确保数据的透明性和不可篡改性。区块链上的每一笔交易都被记录在一个公共账本上，任何人都可以查看。\\n\\n3. **智能合约**：智能合约是运行在区块链上的自动化程序，能够在满足特定条件时自动执行预定的操作。它们使得去中心化应用（DApps）成为可能。\\n\\n4. **加密货币**：Web3 通常与加密货币（如比特币、以太坊）紧密相关。加密货币是 Web3 生态系统中的主要支付手段和价值存储方式。\\n\\n5. **用户主权**：Web3 强调用户对自己数据和资产的控制权。用户可以通过私钥管理自己的数字身份和资产，而不需要依赖第三方平台。\\n\\n6. **去中心化应用（DApps）**：这些是运行在区块链上的应用程序，通常具有去中心化的特性。DApps 可以涵盖各种领域，如金融、社交、游戏等。\\n\\n7. **互操作性**：Web3 旨在实现不同区块链和应用程序之间的互操作性，使得数据和资产可以在不同的平台之间自由流动。\\n\\nWeb3 的目标是创建一个更加公平、透明和用户友好的互联网环境，减少对中心化机构的依赖，赋予用户更多的控制权和隐私保护。',\n",
       " 'response': 'Web3 是互联网发展的一个新阶段，通常被称为“去中心化的互联网”。它基于区块链技术，旨在创建一个更加开放、透明和用户主导的网络环境。以下是 Web3 的一些关键特点：\\n\\n1. **去中心化**：Web3 的核心思想是去中心化，即不依赖于单一的中心化机构或服务器来管理数据和交易。相反，数据存储在分布式的区块链网络中，由多个节点共同维护。\\n\\n2. **区块链技术**：Web3 依赖于区块链技术，这是一种分布式账本技术，能够确保数据的透明性和不可篡改性。区块链上的每一笔交易都被记录在一个公共账本上，任何人都可以查看。\\n\\n3. **智能合约**：智能合约是运行在区块链上的自动化程序，能够在满足特定条件时自动执行预定的操作。它们使得去中心化应用（DApps）成为可能。\\n\\n4. **加密货币**：Web3 通常与加密货币（如比特币、以太坊）紧密相关。加密货币是 Web3 生态系统中的主要支付手段和价值存储方式。\\n\\n5. **用户主权**：Web3 强调用户对自己数据和资产的控制权。用户可以通过私钥管理自己的数字身份和资产，而不需要依赖第三方平台。\\n\\n6. **去中心化应用（DApps）**：这些是运行在区块链上的应用程序，通常具有去中心化的特性。DApps 可以涵盖各种领域，如金融、社交、游戏等。\\n\\n7. **互操作性**：Web3 旨在实现不同区块链和应用程序之间的互操作性，使得数据和资产可以在不同的平台之间自由流动。\\n\\nWeb3 的目标是创建一个更加公平、透明和用户友好的互联网环境，减少对中心化机构的依赖，赋予用户更多的控制权和隐私保护。'}"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res = chain(\"web3是什么\")\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多参数链增加记忆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Created a chunk of size 24, which is longer than the specified 20\n",
      "Created a chunk of size 45, which is longer than the specified 20\n",
      "Created a chunk of size 50, which is longer than the specified 20\n",
      "Created a chunk of size 232, which is longer than the specified 20\n",
      "Created a chunk of size 589, which is longer than the specified 20\n",
      "Created a chunk of size 610, which is longer than the specified 20\n",
      "Created a chunk of size 561, which is longer than the specified 20\n",
      "Created a chunk of size 563, which is longer than the specified 20\n",
      "Created a chunk of size 259, which is longer than the specified 20\n",
      "Created a chunk of size 87, which is longer than the specified 20\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.vectorstores import FAISS\n",
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "\n",
    "e_model = HuggingFaceEmbeddings(\n",
    "    model_name='maidalun1020/bce-embedding-base_v1',\n",
    "    model_kwargs = {'device': 'cpu'},\n",
    "   encode_kwargs= {'batch_size': 64, 'normalize_embeddings': True}\n",
    ")\n",
    "\n",
    "with open(\"letter.txt\") as f:\n",
    "    text = f.read()\n",
    "#切分文本\n",
    "    text_splitter = CharacterTextSplitter(\n",
    "        chunk_size = 20,\n",
    "        chunk_overlap = 5\n",
    "    )\n",
    "    texts = text_splitter.split_text(text)\n",
    "\n",
    "\n",
    "    #使用FAISS向量存储\n",
    "    db = FAISS.from_documents(documents,e_model)\n",
    "    \n",
    "    retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"score_threshold\": 0.5, \"k\": 3})\n",
    "\n",
    "    question = \"name?\"\n",
    "    doc = retriever.get_relevant_documents(question)\n",
    "    print(doc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_36984\\3097761167.py:31: LangChainDeprecationWarning: This class is deprecated. See the following migration guides for replacements based on `chain_type`:\n",
      "stuff: https://python.langchain.com/docs/versions/migrating_chains/stuff_docs_chain\n",
      "map_reduce: https://python.langchain.com/docs/versions/migrating_chains/map_reduce_chain\n",
      "refine: https://python.langchain.com/docs/versions/migrating_chains/refine_chain\n",
      "map_rerank: https://python.langchain.com/docs/versions/migrating_chains/map_rerank_docs_chain\n",
      "\n",
      "See also guides on retrieval and question-answering here: https://python.langchain.com/docs/how_to/#qa-with-rag\n",
      "  chain = load_qa_chain(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new StuffDocumentsChain chain...\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3m下面是一段AI与人类的对话，AI会针对人类问题，提供尽可能详细的回答，如果AI不知道答案，会直接回复'人类老爷，我真的不知道'，参考一下相关文档以及历史对话信息，AI会据此组织最终回答内容.\n",
      "快速传统单体架构  \n",
      "快速完成开发测试部署，运维简单 \n",
      "微服务架构  \n",
      " \n",
      "将传统单体业务架构系统，拆分为更细的粒度，每一个单位都是可以独立的进行设计、开发、测试、部 \n",
      "署，每个服务都可以做到独立的自治，这个拆分出来的单位就是微服务 \n",
      "同时每个拆分出来的单位之间，只能通过 Api接口进行协同或者交互 \n",
      " \n",
      "微服务的核心就是   大拆小 \n",
      "不同的服务是以进程的形式存在的 \n",
      "传统的架构模式下，假如  在界面上看  有摄像头模块、警报记录模块、基础信息模块    ， \n",
      "在   展现层   ，仍然可以看到三个独立的一级菜单，   在用户的角度，就好像是拆分出来的三个独立的模块 \n",
      "或者组件 \n",
      "但是一到了   逻辑层，就会发现所有的业务之间、组件之间   耦合度非常高，   有大量的交叉调用，完全没 \n",
      "有办法去拆开进行独立部署，    同时整个系统都是使用一个数据库，  这就是传统的单体架构 \n",
      " \n",
      "微服务架构，在展现层其实还是一致的，但是在业务上就是大变样，   每个模块之间是独立的，  \n",
      "但是、业务之间必然会存在一定的相互调用，那么此时就必然不可以直接通过跨 DB 进行调用，  只能、 \n",
      "只能、只能通过 API接口调用 ------ 这里就涉及到了  cloud 微服务组件中的   fegin 组件了，该组件可以 \n",
      "实现跨服务调用接口的目的，    \n",
      "但是、想要调用对方服务中的接口，这可是跨服务调用，一个服务怎么能够知道所想要调用的对方暴露 \n",
      "的接口地址   怎么才能知道呢？？？ \n",
      "就需要有一个大前提，  能够相互发现对方服务的存在，  这里就引出了注册中心的重要性，每个服务都需 \n",
      "要先在注册中心中进行注册， -------cloud 微服务组件中的   nacos 组件  就可以完美的充当  注册中心角色\n",
      " \n",
      "一个服务想要调用另外一个服务的接口，就会先从注册中心找到  对方服务中的接口地址，  然后在发起对 \n",
      "接口的调用，  所以在微服务架构中  ，常常会谈所谓的去中心化架构，    接口之间本来是点对点的调用， \n",
      "但是虽然有注册中心这个  只是用来管最基本的控制流的的存在， \n",
      "就算注册中心宕机了，但是由于各个服务之间存在一些地址的缓存，多个微服务之间仍然可以发起组件 \n",
      "之间接口的互相调用 \n",
      " \n",
      "此时，再讲目光转移到前端，前端无论界面展示的是怎么样，要做业务处理，就需要调用后端服务器中 \n",
      "的代码，需要前端发起 http 的 API接口的调用，但是前端是无法感知到服务的存在的   再加上现在项目 \n",
      "都是前后端分离的模式   ，所以一定要加上一层 ======gateway 网关，   通过网关，统一的对所有接口进 \n",
      "行调用以及暴露，在这里给出前端可以访问的 http 访问地址， \n",
      " \n",
      "\n",
      "注意，虽然有了微服务网关，但仍需要 API网关 \n",
      "如果仅仅是微服务网关，那么它的流量的请求代理，只能到微服务的粒度，但是当有了 API网关之后， \n",
      "整个接口的请求流量代理，是可以到一个个 Api服务接口的粒度 \n",
      "[]\n",
      "Human:公司的营销策略是什么？\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input_documents': [Document(metadata={'source': 'wfw.pdf', 'page': 0}, page_content='快速传统单体架构  \\n快速完成开发测试部署，运维简单 \\n微服务架构  \\n\\xa0\\n将传统单体业务架构系统，拆分为更细的粒度，每一个单位都是可以独立的进行设计、开发、测试、部 \\n署，每个服务都可以做到独立的自治，这个拆分出来的单位就是微服务 \\n同时每个拆分出来的单位之间，只能通过 Api接口进行协同或者交互 \\n\\xa0\\n微服务的核心就是   大拆小 \\n不同的服务是以进程的形式存在的 \\n传统的架构模式下，假如  在界面上看  有摄像头模块、警报记录模块、基础信息模块    ， \\n在   展现层   ，仍然可以看到三个独立的一级菜单，   在用户的角度，就好像是拆分出来的三个独立的模块 \\n或者组件 \\n但是一到了   逻辑层，就会发现所有的业务之间、组件之间   耦合度非常高，   有大量的交叉调用，完全没 \\n有办法去拆开进行独立部署，    同时整个系统都是使用一个数据库，  这就是传统的单体架构 \\n \\n微服务架构，在展现层其实还是一致的，但是在业务上就是大变样，   每个模块之间是独立的，  \\n但是、业务之间必然会存在一定的相互调用，那么此时就必然不可以直接通过跨 DB 进行调用，  只能、 \\n只能、只能通过 API接口调用 ------ 这里就涉及到了  cloud 微服务组件中的   fegin 组件了，该组件可以 \\n实现跨服务调用接口的目的，    \\n但是、想要调用对方服务中的接口，这可是跨服务调用，一个服务怎么能够知道所想要调用的对方暴露 \\n的接口地址   怎么才能知道呢？？？ \\n就需要有一个大前提，  能够相互发现对方服务的存在，  这里就引出了注册中心的重要性，每个服务都需 \\n要先在注册中心中进行注册， -------cloud 微服务组件中的   nacos 组件  就可以完美的充当  注册中心角色\\n \\n一个服务想要调用另外一个服务的接口，就会先从注册中心找到  对方服务中的接口地址，  然后在发起对 \\n接口的调用，  所以在微服务架构中  ，常常会谈所谓的去中心化架构，    接口之间本来是点对点的调用， \\n但是虽然有注册中心这个  只是用来管最基本的控制流的的存在， \\n就算注册中心宕机了，但是由于各个服务之间存在一些地址的缓存，多个微服务之间仍然可以发起组件 \\n之间接口的互相调用 \\n \\n此时，再讲目光转移到前端，前端无论界面展示的是怎么样，要做业务处理，就需要调用后端服务器中 \\n的代码，需要前端发起 http 的 API接口的调用，但是前端是无法感知到服务的存在的   再加上现在项目 \\n都是前后端分离的模式   ，所以一定要加上一层 ======gateway 网关，   通过网关，统一的对所有接口进 \\n行调用以及暴露，在这里给出前端可以访问的 http 访问地址， \\n '),\n",
       "  Document(metadata={'source': 'wfw.pdf', 'page': 1}, page_content='注意，虽然有了微服务网关，但仍需要 API网关 \\n如果仅仅是微服务网关，那么它的流量的请求代理，只能到微服务的粒度，但是当有了 API网关之后， \\n整个接口的请求流量代理，是可以到一个个 Api服务接口的粒度 ')],\n",
       " 'human_input': '公司的营销策略是什么？',\n",
       " 'chat_history': [HumanMessage(content='公司的营销策略是什么？', additional_kwargs={}, response_metadata={}),\n",
       "  AIMessage(content='人类老爷，我真的不知道。关于公司的营销策略，通常需要具体了解公司的业务模式、市场定位、目标客户群体等信息才能给出详细的回答。如果您能提供更多关于公司的背景信息，我可以尝试帮助您分析或提供一些通用的营销策略建议。', additional_kwargs={}, response_metadata={})],\n",
       " 'output_text': '人类老爷，我真的不知道。关于公司的营销策略，通常需要具体了解公司的业务模式、市场定位、目标客户群体等信息才能给出详细的回答。如果您能提供更多关于公司的背景信息，我可以尝试帮助您分析或提供一些通用的营销策略建议。'}"
      ]
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# #构建问答对话链\n",
    "from langchain.chains.question_answering import  load_qa_chain\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "             model='deepseek-chat', \n",
    "             openai_api_key=\"sk-e26fe9e686b146e5afcf78a8a07749a4\", \n",
    "             openai_api_base='https://api.deepseek.com',\n",
    "             max_tokens=1024)\n",
    "\n",
    "template = \"\"\"下面是一段AI与人类的对话，AI会针对人类问题，提供尽可能详细的回答，如果AI不知道答案，会直接回复'人类老爷，我真的不知道'，参考一下相关文档以及历史对话信息，AI会据此组织最终回答内容.\n",
    "{context}\n",
    "{chat_history}\n",
    "Human:{human_input}\n",
    "AI:\"\"\"\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    template=template,\n",
    "    input_variables=[\"context\", \"chat_history\", \"human_input\"],\n",
    ")\n",
    "#使用ConversationBufferMemory对对话进行缓存 \n",
    "memory = ConversationBufferMemory(\n",
    "    memory_key=\"chat_history\",\n",
    "    input_key=\"human_input\",\n",
    "    return_messages=True,\n",
    ")\n",
    "\n",
    "#加载对话链\n",
    "chain = load_qa_chain(\n",
    "    llm=llm,\n",
    "    memory=memory,\n",
    "    prompt=prompt,\n",
    "    verbose=True,\n",
    "    chain_type=\"stuff\"\n",
    ")\n",
    "\n",
    "chain({\"input_documents\":docs,\"human_input\":\"公司的营销策略是什么？\"})\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain_llm_2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
